2010-10-11 66 views
4

我現在正在使用PHP 5,並且我非常喜歡在PHP 5中使用OOP。我遇到了一個問題。我幾乎沒有什麼課,功能也很少。很少有函數需要傳遞參數,這些參數是我自己編寫的那些類的對象。參數不是我注意到的嚴格類型。有沒有辦法讓它嚴格鍵入,以便在編譯時我可以使用Intellisense?PHP對象及其功能

例子:

class Test 
{ 
    public $IsTested; 

    public function Testify($test) 
    { 
     //I can access like $test->$IsTested but this is what not IDE getting it 
     //I would love to type $test-> only and IDE will list me available options including $IsTested 
    } 
} 

回答

3

嗯,你可以使用type hinting做你想做什麼:

public function Testify(Test $test) { 

} 

要麼,或文檔塊:

/** 
* @param Test $test The test to run 
*/ 

這取決於IDE,以及它如何拿起類型提示......我知道NetBeans很聰明,可以提取類型提示Testify(Test $test)並讓你從那裏開始,但其他一些IDE並不那麼聰明......因此,這取決於你的IDE,哪個答案會讓你自動完成...

+0

確定我需要給這一個嘗試,會讓你知道 – Neutralizer 2010-10-12 17:47:52

1

我要舉一個簡單的 「不」。回答,然後在PHP文檔中找到關於Type Hinting的部分。

我想那答案。

<?php 
class Test 
{ 
    public $IsTested; 

    public function Testify(Test $test) 
    { 
     // Testify can now only be called with an object of type Test 
    } 
} 

雖然我不確定Intellisense知道類型提示。這完全取決於。

+0

確定我需要給這一個嘗試,會讓你知道 – Neutralizer 2010-10-12 17:47:27

1

$test不是類變量。 也許你想要$this

$this->IsTested; 

OR

public function Testify(Test $test) 
{ 
    $test->IsTested; 
} 
+1

他指的是'$ test', 「證明」的論點。 – Matchu 2010-10-11 13:05:13

+0

@Matchu,看我的編輯。 – pltvs 2010-10-11 13:06:05