Search code examples
phptype-safety

PHP Objects and their Functions


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

Solution

  • 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...