4

我现在正在使用 PHP 5,我很乐意在 PHP 5 中使用 OOP。我遇到了一个问题。我的课程很少,里面的功能也很少。很少有函数需要传递参数,这些参数是我自己编写的那些类的对象。我注意到参数不是严格输入的。有没有办法让它严格输入,以便在编译时我可以使用 Intellisense?

例子:

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
   }
}
4

3 回答 3

3

好吧,你可以使用类型提示来做你想做的事:

public function Testify(Test $test) {

}

无论是那个,还是文档块:

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

这取决于 IDE,以及它如何获取类型提示……我知道 NetBeans 足够聪明,可以获取类型提示Testify(Test $test)并让你从那里开始,但其他一些 IDE 并不那么聪明……所以这真的取决于你的IDE,哪个答案会让你自动完成......

于 2010-10-11T13:03:55.470 回答
1

我打算给出一个简单的“不”。回答,然后在 PHP 文档中找到关于类型提示的部分。

我想这回答了这个问题。

<?php
class Test
{
   public $IsTested;

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

不过,我不确定 Intellisense 是否知道类型提示。这一切都取决于。

于 2010-10-11T13:03:11.647 回答
1

$test不是类变量。也许你想要$this

$this->IsTested;

或者

public function Testify(Test $test)
{
   $test->IsTested;
}
于 2010-10-11T13:04:26.447 回答