How to deprecate PHP's magic property in PHPDoc? How to deprecate PHP's magic property in PHPDoc? php php

How to deprecate PHP's magic property in PHPDoc?


The @mixin approach works at least with PhpStorm:

/** * class or trait for the {@mixin} annotation */trait DeprecatedExampleTrait {    /**     * Declare it as private to increase the warning level     * @deprecated     * @var string     */    public $foo;}/** * Example class * * @mixin DeprecatedExampleTrait * * @property string $newFoo A foo variable. */class Example {    /**     * Magic getter     */    public function __get($var) {        if (in_array($var, ['foo', 'newFoo'])) {            // do & return something        }    }}$example = new Example;$example->foo;

Screenshot:

PhpStorm Screenshot


This is not possible with PHPDoc as the @deprecated can only be associated with structural elements (documentation).

If it is really important for developers to know that they should no longer use this magic property, you could trigger an E_USER_DEPRECATED error:

/** * Example class * * @property string $foo A foo variable. */class Example {    public function __get($name)    {        if ($name === 'foo') {            trigger_error('Property $foo is deprecated and should no longer be used', E_USER_DEPRECATED);        }        // ...    }}


To prevent users from using your deprecated property, you can just remove the PHPDoc for this property from your class header.

/**  * Example class  *  */ class Example {     /**      * Magic getter      */     public function __get($var) {         if('foo' === $var) {             // do & return something         }     }  }

This way, you'll keep your legacy code working, while the property will no longer be shown by IDE autocompletion tools etc.