压倒学说的特质 [英] Overriding Doctrine Trait Properties

查看:66
本文介绍了压倒学说的特质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以通过在课堂上声明一个trait 方法来覆盖它,我很好奇是否可以超越trait Property 道路.这样安全吗?它不在文档中,因此我很犹豫地执行此操作.

I know you can override a trait method by declaring it in your class, I was curious if was possible to over ride a trait Property the same way. Is this safe to do? Its not in the Documentation so I am hesitant to implement this.

摘自文档

An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.

http://php.net/manual/en/language.oop5.traits.php

推荐答案

您不能在使用特征的类中覆盖特征的属性.但是,您可以在扩展使用该特性的类的类中重写特性的属性.例如:

You cannot override a trait's property in the class where the trait is used. However, you can override a trait's property in a class that extends the class where the trait is used. For example:

trait ExampleTrait
{
    protected $someProperty = 'foo';
}

abstract class ParentClass
{
    use ExampleTrait;
}

class ChildClass extends ParentClass
{
    protected $someProperty = 'bar';
}

这篇关于压倒学说的特质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆