Parent Class 中 Trait 的别名方法
Alias method from Trait in Parent Class
我想知道这在 PHP 中是否可行:
trait SomeTrait
{
public function someMethod() { /* ... */ }
}
class Parent
{
use SomeTrait;
}
class Child extends Parent
{
/* Do something to rename someMethod() to someOtherMethod() */
use someMethod as someOtherMethod; // Example
public function someMethod()
{
// Do something different than SomeTrait::someMethod()
}
}
在我的实际use-case中,Parentclass是一个parent到几个children(其中 none 实际上使用了来自应用于 parent class 的特征的 someMethod()
。Parent class 也是一部分一个外部库,所以我不能直接修改源代码。
我实际use-case中的Childclass也依赖于[=43=的受保护属性]class,所以我很确定我需要保留继承。
这实际上可能吗,还是我只需要处理它,并在有问题的 Child class 上使用不同的方法名称?
我昨天用下面的代码实际测试了这个:-)
<?php
trait CanWhatever
{
public function doStuff()
{
return 'result!';
}
}
class X
{
use CanWhatever;
public function doStuff()
{
return 'overridden!';
}
}
$x = new X();
echo $x->doStuff();
echo "\n$x has ";
echo (class_uses($x, 'CanWhatever')) ? 'the trait' : 'no trait';
输出是:
overridden!
$x has the trait
所以保持名称不变只是正常情况下的覆盖。
在这里查看 https://3v4l.org/Vin2H
如果您只想覆盖它,您在问题中得到的内容应该可以正常工作。根据 the manual:
The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.
如果需要在childclass中提供别名,那么可以在childclass中重新use
trait,使用 as
运算符在当前 class:
中定义别名
class Child extends Foo
{
// This will re-import the trait into your child class, aliasing
// any specified methods.
use SomeTrait {
someMethod as someOtherMethod;
}
public function someMethod() {
$this->someOtherMethod();
}
}
您还可以控制新别名的可见性,例如
use SomeTrait {
someMethod as private someOtherMethod;
}
综上所述,我真的看不出这样做有什么好处,你可以直接调用
parent::someMethod();
在 over-riding 之后。但如果你有一个特别复杂的继承树,它可能会有用。
完整示例在这里:https://eval.in/868453
我想知道这在 PHP 中是否可行:
trait SomeTrait
{
public function someMethod() { /* ... */ }
}
class Parent
{
use SomeTrait;
}
class Child extends Parent
{
/* Do something to rename someMethod() to someOtherMethod() */
use someMethod as someOtherMethod; // Example
public function someMethod()
{
// Do something different than SomeTrait::someMethod()
}
}
在我的实际use-case中,Parentclass是一个parent到几个children(其中 none 实际上使用了来自应用于 parent class 的特征的 someMethod()
。Parent class 也是一部分一个外部库,所以我不能直接修改源代码。
我实际use-case中的Childclass也依赖于[=43=的受保护属性]class,所以我很确定我需要保留继承。
这实际上可能吗,还是我只需要处理它,并在有问题的 Child class 上使用不同的方法名称?
我昨天用下面的代码实际测试了这个:-)
<?php
trait CanWhatever
{
public function doStuff()
{
return 'result!';
}
}
class X
{
use CanWhatever;
public function doStuff()
{
return 'overridden!';
}
}
$x = new X();
echo $x->doStuff();
echo "\n$x has ";
echo (class_uses($x, 'CanWhatever')) ? 'the trait' : 'no trait';
输出是:
overridden!
$x has the trait
所以保持名称不变只是正常情况下的覆盖。
在这里查看 https://3v4l.org/Vin2H
如果您只想覆盖它,您在问题中得到的内容应该可以正常工作。根据 the manual:
The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.
如果需要在childclass中提供别名,那么可以在childclass中重新use
trait,使用 as
运算符在当前 class:
class Child extends Foo
{
// This will re-import the trait into your child class, aliasing
// any specified methods.
use SomeTrait {
someMethod as someOtherMethod;
}
public function someMethod() {
$this->someOtherMethod();
}
}
您还可以控制新别名的可见性,例如
use SomeTrait {
someMethod as private someOtherMethod;
}
综上所述,我真的看不出这样做有什么好处,你可以直接调用
parent::someMethod();
在 over-riding 之后。但如果你有一个特别复杂的继承树,它可能会有用。
完整示例在这里:https://eval.in/868453