Discussion:
PHPDoc way to describe the magic getter/setters [SOLVED]
Daevid Vincent
2013-09-25 23:31:45 UTC
Permalink
I use a base.class that most classes extend from. That class uses the lovely
Magic Methods for overloading __get() and __set()
http://php.net/manual/en/language.oop5.magic.php

However (in Zend Studio for example) when I try to auto-assist a property
$foo I don't see that it has a get() or set() method.

I'd like to see something like $this->get_foo() or $this->set_foo() and
also if possible have them show up in the Outline tab window.

Then I randomly stumbled upon this PHPDoc @ method tag and my whole world
is brighter today than it has been for the past, oh let's say DECADE!
http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags
.method.pkg.html
or @property too.
http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags
.property.pkg.html

*giddy!*
(now I just have to go back through all my code and update the class
documentation headers everywhere)

<?php
/**
* This is an example of how to use PHPDoc to describe the magic __get() and
__set()
* so that Zend Studio / Eclipse / Other IDEs can utilize the methods that
don't technically exist.
*
* @method void set_name() set_name(string $name) magic setter for $name
property
* @method string get_name() get_name() magic getter for $name property
*
* @link
http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags
.method.pkg.html
* @link
http://manual.phpdoc.org/HTMLSmartyConverter/PHP/phpDocumentor/tutorial_tags
.property.pkg.html
*/
class foo
{
/**
* @var string $name the description of $name goes here
*/
protected $name;

public function __construct($id = NULL)
{
}
}

$myobj = new foo();

#### Put your cursor after the -> and hit CTRL+SPACE. ####
#### Notice how you have "magic" get_name() and set_name($name) ####
#### appearing and also in the Eclipse "Outline" pane ####

$myobj->

#### You're welcome. ####
?>
David Harkness
2013-09-25 23:37:47 UTC
Permalink
Post by Daevid Vincent
is brighter today than it has been for the past, oh let's say DECADE!
Yes, @method and @property are very handy. Out of curiosity, since you're
providing magic getters and setters, why not use __get and __set instead of
__call with matching on "get_xxx" and "set_xxx"? This would allow using the
simpler (and IMHO much more expressive and PHP-ish) forms

$obj->foo = $obj->bar + 5;

Peace,
David

Loading...