views:

49

answers:

3

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
   }
}
+1  A: 

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.

Matchu
OK I need to give this a try and will let you know
Umair Ashraf
+1  A: 

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

ircmaxell
OK I need to give this a try and will let you know
Umair Ashraf
+1  A: 

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

$this->IsTested;

OR

public function Testify(Test $test)
{
   $test->IsTested;
}
Alexander.Plutov
He's referring to `$test`, the argument of `Testify`.
Matchu
@Matchu, see my edits.
Alexander.Plutov