dourong6054 2016-08-05 15:49
浏览 23

在类属性中扩展类?

Suppose that I've this class:

class Loader 
{
     function library($name)
     {
          require $name . '.php';
     }
}

and now I include the class foo ($name) inside my controller, like this:

class Controller
{
    function __construct()
    {
         $this->load = new Loader();
    }
}

class Child_Controller extends Controller
{
    function __construct()
    {
         parent::__construct();
         $this->load->library('foo'); 
         init();
    }
}

is possible, for example, access to the included class inside $this? Like:

class Child_Controller extends Controller
{
    //..construct above..

    function init()
    {
        $this->print('some text');
    }
}

where print is a method of foo, the class included:

class Foo
{
    function print($message)
    {
         echo "your message: " . $message;
    }
}

So, summing, I want include in the child controller, in $this, all the method of the included class by the Loader class extended by the base controller. Is this possible?

Or another idea would be create dynamically in the Child_Controller, a property that take the name of the included class, so, for call the method of foo I can do something like:

$this->Foo->print('some text');
  • 写回答

1条回答 默认 最新

  • duandai2178 2016-08-05 15:55
    关注

    No, you can't do that however you can do something similar in storing Foo in a generic property like this:

    class Child_Controller 
    {
        private $lib;
    
        function __construct()
        {
            parent::__construct();
            $this->lib = $this->load->library('Foo');
            $this->init();
        }
    
        function init()
        {
            $this->lib->print('Hello World!');
        }
    }
    

    If you want to instantiate multiple libraries then rather than using a dynamic name for the variable you should use an array with the library name as the key, like this:

    class Child_Controller 
    {
        private $libs = [];
    
        function __construct()
        {
            parent::__construct();
            $this->libs['Foo'] = $this->load->library('Foo');
            $this->init();
        }
    
        function init()
        {
            $this->libs['Foo']->print('Hello World!');
        }
    }
    

    If you can't use an array and absolutely must use a variable name you can do that like this:

    class Child_Controller 
    {
        function __construct()
        {
            $var = 'Foo';
            $this->{$var} = $this->load->library($var);
            $this->init();
        }
    
        function init()
        {
            $this->Foo('Hello World!');
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。