2012-12-14 30 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(); 
相關問題