2010-11-11 30 views
-2

PHP的define("CONSTANT", "Hello world.");是全局函數,即使你在類或函數內部,你也可以調用它。我在下面的例子中,讓我們說,在課堂內我宣佈一個全球變量應該像私人全球$allForm;php裏面的全局執行

我的問題如何在validate()中調用$ this-> allForm而不在$ _-construct中嵌入$ this-> validate()?

$authentication = new authentication("1", "nanat", "amew", "yes"); // declared define constant 

class authentication extends mySession { 
    private $allForm; //variable 

    public function __construct($submit, $user, $password, $remmeber) { 
     $this->allForm = array('giSub' => $submit, 'giUser' => $user, 'giPas' => $password, 'giRemmeb' => $remmeber); //execute 
     // $this->validate(); //not execute 
    // $this->validateOne(); //not execute 
    // $this->validateTwo() //not execute 
    } 

    private function validate() { 
     var_dump($this->allForm); // return null 
     // statement... 
    } 

private function validateOne() { 
     var_dump($this->allForm); // return null 
     // statement... 
    } 

private function validateTwo() { 
     var_dump($this->allForm); // return null 
     // statement... 
    } 


} 

我想要的是..這可能嗎?

class authentication extends mySession { 
    private $allForm; //global variable.. 


    public function __construct($submit, $user, $password, $remmeber) { 
     $this->allForm = array('giSub' => $submit, 'giUser' => $user, 'giPas' => $password, 'giRemmeb' => $remmeber); //execute 
    } 

    private function validate() { 
     var_dump($this->allForm); // return true 
     // statement... 
    } 

private function validateOne() { 
     var_dump($this->allForm); // return true 
     // statement... 
    } 

private function validateTwo() { 
     var_dump($this->allForm); // return true 
     // statement... 
    } 


} 
+0

對不起,什麼?你能試着用例子來解釋嗎? – deceze 2010-11-11 02:18:19

+0

這樣做會達到什麼目的?你已經將代碼封裝在一個使用私有'$ allForm'的類中。優秀。 – Michael 2010-11-11 02:22:27

+0

有沒有一種方法可以設置一個全局變量而不用調用$ this-> validate();?爲了執行$ this-> allForm?成像你有5功能,那麼你將包括__construct內的所有功能,以執行this-> allForm? – mapet 2010-11-11 02:24:26

回答

2

您無需立即致電$this->validate()從構建。如果在構造函數中填充$this->allForm,則在調用類方法時,該值將繼續存在。刪除從__construct()$this->validate(),你可以手動調用validate()方法:

$authentication = new authentication("1", "nanat", "amew", "yes"); 
$authentication->validate(); 

基於你問這個問題的辦法,這是最好的答案我可以提供。

+1

+ +1嘗試回答這個問題。請注意,您需要將'validate'方法標記爲'public'來執行您的建議。 – deceze 2010-11-11 02:37:05

+0

有沒有像php difine ..在課堂內的功能?可以是一個全局變量並且可以訪問所有的froms函數?彼此沒有約束? – mapet 2010-11-11 02:41:29

+0

@mapet對不起,你的問題沒有意義。你能用代碼來更新你的問題嗎?這些代碼演示你想要做什麼*?代碼不需要工作,它應該只顯示你想要達到的結果。 – deceze 2010-11-11 02:46:59

1

$allForm被宣佈爲private的成員class authentication。這意味着您只能從內部的class authentication中訪問$allForm。如果您想從以外的訪問$allForm該課程,則需要將其聲明爲public

您也可以將相同的推理應用於validate()方法。如果您希望能夠從稱之爲外類,那麼就宣告它public

class authentication extends mySession { 
    public $allForm; 

    ... 

    public function validate() { 
     ... 
    } 
} 

現在你可以做這樣的東西....

$authentication = new authentication("1", "nanat", "amew","yes"); 
$authentication->validate(); 

OR

$authentication->allForm = array(....);