2010-01-11 63 views
0

任何人都可以告訴我可以包含在構造函數中嗎?我可以在PHP的構造函數中添加什麼?

我知道我可以做以下事情。

function __construct(){ 
    parent::Controller(); 
    session_start(); 

    } 

但我想知道如果我能提前

由於添加任何變量,if語句等。

+2

你有沒有嘗試*聲明變量或使用'if'? – 2010-01-11 18:53:24

回答

1

敲你自己。添加你想要的任何PHP。您可以使用$this來引用正在創建的對象。

1

你可以在你的默認構造函數中包含變量,函數調用,方法調用,對象聲明等等等。

class Test { 

    protected $protected; 
    private static $static; 

    function __construct() { 
     parent::__construct(); 
     $this->protected = 'test'; 
     $variable_local = 'hey'; 
     self::$static = 'im static'; 
     $obj = new OtherClass(); 
     $this->myMethod(); 
     externalFunction(); 
    } 

    public function myMethod() { 
     echo 'all mine'; 
    } 

} 

function externalFunction() { 
    'hey, im external'; 
} 
+0

謝謝。我可以在視圖中調用echo $ variable_local嗎?如何在視圖中調用$ this-> protected? – shin 2010-01-11 19:20:55

相關問題