문제

I am using PHP 5 now and I am exuberant to use OOP in PHP 5. I encounter a problem. I got few classes and few functions inside them. Few functions require arguments to be passed which are object of those classes I wrote myself. Arguments aren't strictly typed I noticed. Is there a way to make it strictly typed so that at compile time I could use Intellisense?

Example:

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
   }
}
도움이 되었습니까?

해결책

Well, you could use type hinting to do what you want:

public function Testify(Test $test) {

}

Either that, or the docblock:

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

It depends on the IDE, and how it picks up the type hints... I know that NetBeans is smart enough to pick up the type-hint Testify(Test $test) and let you go from there, but some other IDEs are not that smart... So it really depends on your IDE which answer will get you the autocompletion...

다른 팁

I was going to give a simple "No." answer, then found the section on Type Hinting in the PHP docs.

I guess that answers that.

<?php
class Test
{
   public $IsTested;

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

I'm not sure Intellisense knows about type hinting, though. That all depends.

$test isn't class variable. Maybe you want $this?

$this->IsTested;

OR

public function Testify(Test $test)
{
   $test->IsTested;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top