Thursday 31 March 2016

Init class of a unnamed namespace inside other namespace

Hello,

How to init a class without namespace in another class with namespace:

<?php

class first_class {
   public function  __construct() {
   }
}

And:

<?php

namespace namespace1\namespace2;

class second_class {
         public function  __construct() {
         }

         public function test() {
                $object = first_class(); --> ERROR!!!!
                $object = \first_class(); --> OK
         }
}

Best regards!

Wednesday 30 March 2016

Moodle: Get courses recursively

Hello,

How to get all courses of a category recursively?

<?php

global $CFG;
require_once($CFG->dirroot . '/lib/coursecatlib.php');
$categoryid = 1;
$allcourses = coursecat::get($categoryid)->get_courses(array('recursive' => true));

How to get all courses of a hidden category?

<?php

global $CFG;
require_once($CFG->dirroot . '/lib/coursecatlib.php');
$categoryid = 1;
$allcourses = coursecat::get($categoryid, MUST_EXIST, true)->get_courses(array('recursive' => true));

Best regards!

Thursday 3 March 2016

PHP : How to access to an object variable whose name is another variable of the object

Hello,

If we suppose that:

<?php
class test_class
{
       private $variable_name = 'id';
       private $id  = 0;
}

How to acces to variable 'id' inside an instance? :

<?php

class test_class
{
       private $variable_name = 'id';
       private $id  = 0;

       function test() {
             echo $this->$this->variable_name;  // ERROR!!!!
             echo $this->{$this->variable_name};
       }
}

$object = new test_class();

$object->test();

Best regards!