Search code examples
phpphpdoc

How to deprecate PHP's magic property in PHPDoc?


Is there any way to mark a magic property as deprecated? Consider following, simplified code:

/**
 * Example class
 *
 * @property string $foo A foo variable.
 */
class Example {
    /**
     * Magic getter
     */
    public function __get($var) {
        if('foo' === $var) {
            // do & return something
        }
    } 
}

Now, how to indicate other developers, that they should not use Example::$foo anymore? The only working solution that comes to my mind is:

/**
 * Example class
 */
class Example {
    /**
     * A foo variable.
     *
     * @var string
     * @deprecated
     */
    public $foo;

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

But this both breaks my code (getter is not called) and doesn't feel very elegant.


Solution

  • 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