6

Is is possible (for type hinting purposes) to indicate the class of an object's property? The final product of the code works as expected, but, type hinting does not. For example, the following shows what I am trying to accomplish but that does not work:

    /* @var $this->view \app\components\View */
    $this->view->title = 'Title here';

All I have ever seen were references to @var $this or similar, but never a property of the $this object. If this is possible, what am I missing?

1 Answer 1

4

Yes - you must apply the PHPDoc annotation where you declare the property in the class:

class Foo
{
    /* @var \app\components\View $view*/
    public $view;

    public function __construct()
    {
          $this->view->title = 'Title here';
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to annotate it in the class without declaring it? Such as when class Foo extends class Bar which is where the property is declared? I would have to go back through a few classes to find the original declaration of the view property (through vendor code, that is).
I'm not aware of any ways to do that. I think I'd just opt to redeclare the property in the super class if your IDE was giving you fits.
Since the super class is outside of my control that won't be easy/viable. What is working, though, is redeclaring the property in my class then inside where it is used I set it equal to the parent implementation of the value. So, my class starts off with it empty but by the time it is used it is back to what it should be. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.