I am a HUGE fan of code completion in my IDE and a simple rule of thumb that I use is if I actually have to look at the class I've written to determine how to use it, I've written a shitty API. However there are many times you may use a factory method or something similar to return an object and your completion is broken since the IDE doesn't know what it's hinting.
Well I was playing around the other day and realized that if you use the PHPDocumentor format for your method that returns your object, it will use code completion.
So for an example.
Today I created a PHP Validation class and wanted code-completion for my chaining.
Here is a really dumbed down version to simply illustrate the issue.
<?php class Validate { private $field; private $value; private $errors = array(); public function setField($field_name, $value) { $this->field = $field_name; $this->value = $value; return $this; } public function minLength($minimum_character_count) { if(strlen($this->value) <= $minimum_character_count) { $this->errors[$this->field] = "$this->field must contain at least $minimum_character_count characters."; } return $this; } public function maxLength($maximum_character_count) { if(strlen($this->value) >= $maximum_character_count) { $this->errors[$this->field] = "$this->field can contain a maximum of $maximum_character_count characters."; } return $this; } } ?>
Now to add a field that is named 'name' and has my name as a value I would do.
<?php $V = new Validate(); $V->setField("name", "Kevin Korb")->minLength(3)->maxLength(255); ?>
The problem with this is once I do:
$V->setField("name", "Kevin Korb")->
I want my methods for Validate to pop-up again in Zend Studio, however they dont.
But... When I add my comments like this...
<?php class Validate { private $field; private $value; private $errors = array(); /** * Set Field To Validate * * @param string $field_name * @param mixed $value * @return Validate object */ public function setField($field_name, $value) { $this->field = $field_name; $this->value = $value; return $this; } /** * Minimum Length Validation * * @param int $minimum_character_count * @return Validate Object */ public function minLength($minimum_character_count) { if(strlen($this->value) <= $minimum_character_count) { $this->errors[$this->field] = "$this->field must contain at least $minimum_character_count characters."; } return $this; } /** * Max Length Validation * * @param int $maximum_character_count * @return Validate Object */ public function maxLength($maximum_character_count) { if(strlen($this->value) >= $maximum_character_count) { $this->errors[$this->field] = "$this->field can contain a maximum of $maximum_character_count characters."; } return $this; } } ?>
Let's see what happens now...


AWESOME!!!!

3 comments to "Zend Studio 5 Code Completion For Objects Returned From Methods"
Unfortunately I'm not sure how you would accomplish this on 6 or 7. Eclipse and I don't get along very well.
Leave a Comment