views:

177

answers:

2

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 objects, but ignoring this, what would the correct phpdoc syntax be to specify a specific class for the $this object?

+1  A: 

You can define it like this:

/* @var $this type */

where type is a class name

David Caunt
Thanks, sounds good, but doesn't work in Aptana - it recognises $this as a keyword and won't link it to the defined var type
benz001
A: 

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

ashnazg
In this instance my issue arises from the template system within the framework. The template's are essentially snippets of php code that only get given a context at runtime. So I'm essentially looking for a syntax to 'fool' the Aptana IDE into setting the scope for $this to a particular class type, in the Magento scenario all templates derive from Mage_Core_Block_Template so being able to link them to that would be a great start, in most cases you could go one step better and link template x to Custom_X_Block_Template safely.
benz001