题
我现在正在使用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
}
}
解决方案
好吧,你可以使用 类型提示 做你想做的事:
public function Testify(Test $test) {
}
要么,要么是Docblock:
/**
* @param Test $test The test to run
*/
它取决于IDE,以及它如何拾取类型提示...我知道Netbeans足够聪明,可以拾取类型的鉴定 Testify(Test $test)
然后让您从那里走,但是其他一些IDS并不那么聪明...因此,这实际上取决于您的IDE,哪个答案可以使您获得自动完成...
其他提示
我打算给出一个简单的“不”答案,然后找到有关 类型提示 在PHP文档中。
我想那回答了。
<?php
class Test
{
public $IsTested;
public function Testify(Test $test)
{
// Testify can now only be called with an object of type Test
}
}
我不确定Intellisense是否知道类型提示。这一切都取决于。
$test
不是类变量。也许你想要 $this
?
$this->IsTested;
或者
public function Testify(Test $test)
{
$test->IsTested;
}
不隶属于 StackOverflow