Hello, I was playing with PHP classes and inheritance and I found out this little trick. With this method we can create a big super object(in the code is called base) and a class extending this super object can also access other classes created by this super object.
BUT is this a good practice to follow or not? Maybe if we want to plug a module to our code
PHP Code:class Base {
function __construct($what)
{
//class always instantiated
$var = 'A';
$low_var = strtolower($var);
$this->$low_var = new $var;
//-------------------------
//optional classes instantiated
foreach($what as $var)
{
$low_var = strtolower($var);
$this->$low_var = new $var;
}
}
}
class A {
function __construct(){
echo ' a | ';
}
function test(){
echo 'test function from class a';
}
}
class B {
function __construct(){
echo ' optional b | ';
}
}
class theClass extends Base {
function __construct(){
$what[0] = 'B';
parent::__construct($what);
$this->a->test();
}
}
$tmp = new theClass();






Bookmarks