2010-10-11 122 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