Question

So the following is a weird issue and while I'm sure people are going to accuse me of bad practice when it comes to how this behavior is happening, for the sake of the project I'm working on, this is the use case, and I'd love to know if I'm missing something.

I have the following situation

 class foo()
 {
      private $_bar;

      public function __construct($bar)
      {
           $this->_bar = $bar;
      }

      public function doStuff()
      {
           include('doStuffCode.php');
      }
 }

doStuffCode.php:

 $this->_bar->(this is where I'd like to get some type hinting)

I've tried to drop in /* var $this->_bar Bar */ but that doesn't seem to do anything for me. It seems like I can't add type-hinting to a specific class variable. The workaround for now has been to set up a temporary variable called $bar, set the type hinting on it, then use it to get the methods I'm looking for and copying them back into the right context.

I'm curious if I'm just trying something wrong, doing something unsupported, misunderstanding how I should be declaring the javadoc, or have a fundamental misunderstanding of how this type-hinting should be working.

Thanks for the help

Was it helpful?

Solution

You need to define the type of $_bar in the class

  class foo()
  {
        /**
         * @var Bar
         */
        private $_bar;

        public function __construct($bar)
        {
             $this->_bar = $bar;
        }

        public function doStuff()
        {
             include('doStuffCode.php');
        }
   }

And then in your included file you hint:

/* @var $this foo */

Which will be the correct way of doing this. All though I'm not sure private variables can be hinted this way due to the scope of the included file contents.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top