Devon Bessemer
Devon Bessemer

Reputation: 35337

Properly documenting a variable that links to class in phpdoc

How would I properly document this in phpdoc?

I have a variable that links to a separate class, like so:

public $link;
public function link($class) {
   $class = "\\Path\\To\\Class\\$class";
   $this->link = new $class;
}

Would I implement the phpdoc tag at the variable level or the function level? Along with learning the proper implementation of phpdocs, I am trying to make it so the code is recognizable in IDEs like PHPStorm/Netbeans.

Upvotes: 1

Views: 455

Answers (1)

ashnazg
ashnazg

Reputation: 6688

This layout is too "runtime-dynamic" to document. Since there's no way to know what one class the $class argument points to, there's no way for the $link property to know which class to list as its datatype. This means no IDE will be able to figure anything out for you.

The only way to "document" it would be this, which is accurate, but gives your IDE nothing to go on:

/** @var mixed */
public $link;
/**
 * @param string $class
 * @return void
 */
public function link($class) {
   $class = "\\Path\\To\\Class\\$class";
   $this->link = new $class;
}

Upvotes: 1

Related Questions