有没有办法将魔法属性标记为已弃用?考虑以下简化代码:
/**
* Example class
*
* @property string $foo A foo variable.
*/
class Example {
/**
* Magic getter
*/
public function __get($var) {
if('foo' === $var) {
// do & return something
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如何指出其他开发者,他们不应该再使用Example::$foo了?我想到的唯一可行解决方案是:
/**
* Example class
*/
class Example {
/**
* A foo variable.
*
* @var string
* @deprecated
*/
public $foo;
/**
* Magic getter
*/
public function __get($var) {
if('foo' === $var) {
// do …Run Code Online (Sandbox Code Playgroud) 我们假设我有一个模型接口:
interface Model
{
}
Run Code Online (Sandbox Code Playgroud)
以及接收该模型作为参数的接口中的函数:
interface Service
{
public function add(Model $model);
}
Run Code Online (Sandbox Code Playgroud)
为什么,当我用另一个实现上述功能的Model实现该服务时,如下所示:
class AnotherModel implements Model
{
}
class AnotherService implements Service
{
public function add(AnotherModel $model);
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
致命错误:AnotherService :: add()的声明必须与Service :: add(Model $ model)兼容