NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
[resolved] Code completition and factory pattern?


Joined: 18 Sep 2007
Posts: 54
Location: Croatia
Reply with quote
Hi,

I have a question regarding code completition inside scripts that use object made by factory pattern. Here is small example to clarify question.

Code:

interface myInterface {
 public function method1();
 public function method2();
}
abstract class myAbstract {
 protected $property;
 protected function __construct($args){
  // process args...
 }
}
class myFactoryClass extends myAbstract {
 public static function setupObject($args){
  switch($args){
   case 'myType1':
     return new myClassType1($args);
     break;
   case 'myType2':
     return new myClassType2($args);
     break;
   // Type definition continues...
  }
 }
}
class myClassType1 extends myAbstract implements myInterface {
 // Interface, methods, etc...
}
class myClassType2 extends myAbstract implements myInterface {
 // Interface, methods, etc...
}


So when I setup object, factory will use right class for everything else:
Code:

$object = myFactoryClass::setupObject('myType1');


The thing that puzzles me is when I use class in my script it is always showing methods from myClassType2 for instance, no matter which class was really used in factory for making an script object. This is not really a problem since all of them implements same interface, but it can be sometimes confusing.

Am I doing something wrong, or does PhpEd use first/last class that it can see to produce list for code completition?[/code]
View user's profileFind all posts by konixnetSend private messageVisit poster's website


Joined: 14 Jun 2007
Posts: 67
Reply with quote
Try using PHPDoc hints:

Code:

/** @var myClassType1 */
$object = myFactoryClass::setupObject('myType1');
View user's profileFind all posts by CjungeSend private message


Joined: 18 Sep 2007
Posts: 54
Location: Croatia
Reply with quote
...and it works Smile
Thank you very much for the help.
View user's profileFind all posts by konixnetSend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
that's correct and works the best if your method returns different instances (depending on the argument for example).

If it returns instances of only one class, it's easier to find setupObject() methd and add /** @returns myClassType1 */ before it.
In this case you will not have to bother with every and each call.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 18 Sep 2007
Posts: 54
Location: Croatia
Reply with quote
Thnx for the info dmitri.
Is there a quick reference sheet what can you do with phpDoc and how it effects PHPEd's auto competition?
View user's profileFind all posts by konixnetSend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
please check phpdoc manuals available online.
PhpED recognizes everything that may affect code completion: @var, @param and @return

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website
[resolved] Code completition and factory pattern?
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic