2013-04-01 92 views
0

我想更好地理解函數調用在OOP中的工作方式。我有以下樣品:在另一個函數中從類調用函數

class SomeClass { 
    function __construct(){ 
     //run function do() 
     //run function include() 
     //run function run() 
    } 

    public function do($foo){ 
     //do some stuff 
    } 

    public function include(){ 
     require_once(CONSTANT . 'required.php'); 
    } 

    public function run(){ 
     required_func(); 
    } 
} 

$load_class = new SomeClass(); 

在required.php:

function required_func(){ 

    $customerInfo = "info"; 
    $customer = $this -> do($customerInfo); //--> This isn't right 
    return $customer; 
} 

我試圖做的是有required_func()運行DO()與$ customerInfo。所以基本上:如何從包含在require_once文件中的另一個函數調用一個Class公共函數?我甚至在這裏的軌道上遠程?

感謝您的幫助

+0

通$這在調用required_func(),然後接受$ customerObject作爲參數在required_func()和調用do()對$ customerObject –

回答

1

$這不是在作用域功能required_func()

class SomeClass { 
    function __construct(){ 
     //run function do() 
     //run function include() 
     //run function run() 
    } 

    public function do($foo){ 
     //do some stuff 
    } 

    public function include(){ 
     require_once(CONSTANT . 'required.php'); 
    } 

    public function run(){ 
     required_func($this); 
    } 
} 

$load_class = new SomeClass(); 

function required_func($customerObject){ 

    $customerInfo = "info"; 
    $customer = $customerObject->do($customerInfo); 
    return $customer; 
} 
+0

謝謝,它的工作原理。所以基本上$ customerObject傳遞給類的$這個? –

+0

Nevermind :)剛纔看到你對這個問題的評論...謝謝 –