开发者

What is the phpdoc syntax to link $this to a specific class in Aptana?

开发者 https://www.devze.com 2022-12-21 00:03 出处:网络
I\'m working on Magento templates, but this issue would apply to any template loading system. As these templates are loaded by the template engine there\'s no way for the IDE (in this case Aptana) t

I'm working on Magento templates, but this issue would apply to any template loading system.

As these templates are loaded by the template engine there's no way for the IDE (in this case Aptana) to know what object type $this is.

Potentially it could more than one object as a single template could be loaded by multiple obje开发者_开发知识库cts, but ignoring this, what would the correct phpdoc syntax be to specify a specific class for the $this object?


You can define it like this:

/* @var $this type */

where type is a class name


To be clear, using $this should only ever indicate an object of the current class, right?

PhpDocumentor doesn't currently (v1.4.3) recognize $this as a specific keyword that should equate to a datatype of the class itself.

Only datatypes known by PHP and classes already parsed by PhpDocumentor are the proper datatype values to use with the @return tag. There is a feature request in to have some option available in PhpDocumtentor to aid in documenting fluent methods that always "return $this". [1]

In the case of the @var tag, I don't see how it would be feasible for a class variable to contain its own class instance. As such, I can't follow what "@var $this" should be saying.

If, however, your intention with $this is not for fluent methods that "return $this", and was simply to be some shortcut to PhpDocumentor and/or your IDE to magically guess what datatypes you might mean by using $this, I'd have to guess there's no way to do it. The closest suggestion I could make would be to use the name of a parent class that is a common parent to all the various child classes that this particular var/return might be at runtime, and then use the description part of the tag to have inline {@link} tags that list out the possible child classes that are possible.

Example: I have a Parent abstract class with Child1, Child2, and Child3 children that each could occur in my runtime Foo class.

So, Foo::_var could be any of those child class types at runtime, but how would I document this?

/**
 * @var Parent this could be any child of {@link Parent}, {@link Child1}, {@link Child2}, or {@link Child3}...
 */
protected $_var;

Getting back to the "return $this" issue, I'd document things in a similar way:

/**
 * a fluent method (i.e. it returns this class's instance object)
 * @return Parent this could be any child of {@link Parent}, {@link Child1}, {@link Child2}, or {@link Child3}...
 */ 
public function foo() {
   return $this;
}

Documenting this way at least allows your class doc to have links to the particular classes. What it fails to do is highlight the fluent 'ness. However, if your IDE is capable of recognizing the class names, then perhaps it will be able to do the necessary logical linking to those other classes. I think Eclipse is able to do this at least with popup help, if you hover over the class name in the tag's description. I do not think Eclipse can use this to then make the various child classes' methods available in code completion. It would know about the Parent methods for code completion, because the datatype I explicitly list is Parent, but that's as far as the IDE can go.

[1] -- http://pear.php.net/bugs/bug.php?id=16223


I have found that defining a type with @var for $this does not work - presumably because $this is special and is treated as such by Aptana. I have a similar need to the poster I think - it is in template files (in my case simply located and included by functions within the data class) that I wish to set a type for $this. As @ashnazg says, setting a type for $this within a class definition is not needed, because the type of $this is always the type of the class (up to inheritance).

There is, however, a workaround for template files. At the top of the template file simply put something like

/**
 * @var My_Data_Model_Type
 */
$dataModel = &$this;

Then simply use $dataModel (or whatever you choose to call it - maybe something shorter) instead of $this in the template

0

精彩评论

暂无评论...
验证码 换一张
取 消