2012-04-19 96 views
0

这是我的代码,自动加载并调用/实例化我的班自动加载类

public function __get($name) 
{ 
    $classname = '\\System\\' . ucfirst($name); 

    if (property_exists($this, '_' . $name)) 
     $this->{'_' . $name} = new $classname(); 
    else 
     echo $name . ' isn\'t a valid property.'; 
} 


private function boot() 
{echo "<pre/>"; 
    spl_autoload_register(null, false); 

     if (function_exists('__autoload')) 
      spl_autoload_register('__autoload'); 

     spl_autoload_register(array($this, 'libraries')); 
var_dump($this->helper); 
     //$this->configuration(); 
} 

当我打电话$this->helper我得到这个错误

Fatal error: Call to a member function test() on a non-object in (...) 

所以我问题是,在调用__get()方法时,这些类是否仍在加载?

是的,test()确实的方法,我Helper

+0

这一定是地狱维持,当它是一个完全成熟的项目。 – 2012-04-19 21:50:52

回答

0

存在应该$this->_helper->test();为你的代码是这样做的:$this->{'_' . $name} = new $classname();

你的魔术方法会在每次调用时重新实例化类。你应该有它检查它是否是null第一:

if (property_exists($this, '_' . $name) && $this->{'_' . $name} !== null) 
+0

会'is_null()'工作一样吗?因为我想实例化,如果属性不是空的权利? – Eli 2012-04-19 22:16:10

+0

是的,'is_null'具有相同的效果。 – webbiedave 2012-04-19 22:36:57