I have this code below,

Loader.php

namespace Loader;

class Loader{
    public function load($name){

        $module_name = ucfirst($name);

        require_once(__DIR__.'/'.$module_name.'.php');

        return new $module_name;
    }
    public function parentFunction(){
       return get_class($this);
    }
}

$loader = new Loader;

Captcha.php

namespace Loader;

class Captcha extends Loader{
    public function callParentFunction(){
        var_dump($this->parentFunction());
    }
}

and test.php

require_once(__DIR__.'/Loader.php');


$loader->load('captcha')->callParentFunction();

if I create instance of the child class manually by

return new Captcha;

and it works, but if I do

return new $module_name;

it throws me this error,

Fatal error: Class 'Captcha' not found in

any help, ideas please?

0 Answers