A
A
Alexander Wolf2014-04-14 20:25:48
PHP
Alexander Wolf, 2014-04-14 20:25:48

How to define an instance of a class in PHPDoc?

Hello! I use PHPStorm and write PHPDoc style comments:

class User {
  static function getWolF() {
    $class = 'WolF';
    $class = new $class();
    return $class;
  }
}

class WolF {
  public function test($a, $b) {
    return $a + $b;
  }
}

class Hope {
  /**
   * @var WolF
   */
  protected $j;

  public function jaz() {
    $this->j = new User();
    $this->j = $this->j->getWolF();

    $this->j->test();
  }
}

So, in PHPStorm it $this->j->test();is highlighted in red (it says that there is no test method).
I can change everything except the User class.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Confl1kt, 2014-04-14
@mannaro

I can change everything except the User class.
And, more precisely, there each time a different type is returned.

well then return classes need an interface or an abstract class
and magic is bad

C
Confl1kt, 2014-04-14
@Confl1kt

/**
* @return Wolf
*/
static function getWolF() {
    $class = 'WolF';
    $class = new $class();
    return $class;
  }

C
Cage, 2014-04-14
@Cage

If the property is not accessed from an object method and, accordingly, the property would be declared as public, then it would be possible to write in the dock for the class

/**
* @property WolF $j
*/
class Hope {

  public $j;

...
}

But neither for protected properties nor when accessing a magic property inside class methods does this work.
Try to make a description for the received data, something like this:
public function jaz() {
    $user = new User();
    $j = $user->getWolF();
/**
* @var $j WolF
*/
    $this->j = $j;
    $this->j->test();
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question