Magic Method Support for PHP

I've officially switched over to PhpStorm as my primary PHP IDE. I'm loving many of its features, it's excellent. I love the fact that it can detect the usage of undeclared object vars and methods... But this can pose a problem in some cases. For example:

 $this->dom = new DOMDocument;
 $this->dom->formatOutput = true;

PhpStorm is marking formatOutput as an undefined field. But it's built-in to PHP. However that's not the only issue, that's actually a minor one. One of my biggest issues is that many codebases I work on use PHP's magic methods, specifically __get(), __set() and __call(). PhpStorm doesn't appear to support those because, for example:

$zend_console_getopt = new Zend_Console_Getopt(array(
     ....
     "help|h" => "Show this help message"
));
if ($zend_console_getopt->help)
{
     ...
}



It's marking help as an undefined field. Now I understand that it would be a huge task for PhpStorm to be able to autodetect this kind of thing, but it should at least mark undefined fields and methods in classes that have implementations of the magic methods differently, perhaps as a separate kind of inspection. Ideas?
6 comments
Comment actions Permalink

Oh, nearly forgot to mention. Support for setting undefined fields in an stdClass object shouldn't be marked, but maybe it can detect when they haven't been declared yet?

0
Comment actions Permalink

And something else I've just discovered...

I'm using Smarty in one project and have extended it, like so:

class SmartyMain extends Smarty



Then I use this:

$smarty = new SmartyMain;
$smarty->assign(...);



It's marking the assign function as non-defined...
0
Comment actions Permalink

You can use the PHPDoc @property comments:

http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.property.pkg.html

However, it seems that phpStorm does not support the mention of a variable-type

0
Comment actions Permalink

>However, it seems that phpStorm does not support the mention of a variable-type :(
Please elaborate on this PhpStorm supports @property tags just as described in the page you've pointed at AFAIK

0
Comment actions Permalink

I've found the @method described in phpDoc manual useful in solving similar problems in 2017 version of IntelliJ IDEA (and others mentioned similar results for PHPStorm - also later versions, IDEA included).

http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.method.pkg.html

@method works for __call() while the @property mentioned in a 2010 post above works for __get() and __set() magic methods.

http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.property.pkg.html

0

Please sign in to leave a comment.