2012-12-14 33 views
0

我有一个问题,可能不适合大多数人。 很抱歉,如果它是你明显...内一类:从函数B访问函数A

这是我的代码:

class Bat 
{ 
     public function test() 
     { 
     echo"ici"; 
     exit(); 
     } 

     public function test2() 
     { 
     $this->test(); 
     } 
} 

在我的控制器:

bat::test2(); 

我有一个错误:

Exception information: Message: Method "test" does not exist and was not trapped in __call()

+1

是什么FbHelper与蝙蝠呢? –

回答

2

Bat :: test2引用了一个静态函数。所以你必须声明它是静态的。

class Bat 
{ 
     public static function test() 
     { 
     echo"ici"; 
     exit(); 
     } 

     // You can call me from outside using 'Bar::test2()' 
     public static function test2() 
     { 
     // Call the static function 'test' in our own class 
     // $this is not defined as we are not in an instance context, but in a class context 
     self::test(); 
     } 
} 

Bat::test2(); 

否则,你需要的Bat的实例并调用该实例的功能:

$myBat = new Bat(); 
$myBat->test2(); 
相关问题