美文网首页
traits vs inheritance is that me

traits vs inheritance is that me

作者: 张霸天 | 来源:发表于2017-03-24 18:01 被阅读0次

Another difference with traits vs inheritance is that methods defined in traits can access methods and properties of the class they're used in, including private ones.

For example:
<?php
trait MyTrait
{
  protected function accessVar()
  {
    return $this->var;
  }

}

class TraitUser
{
  use MyTrait;

  private $var = 'var';

  public function getVar()
  {
    return $this->accessVar();
  }
}

$t = new TraitUser();
echo $t->getVar(); // -> 'var'                                                                                                                                                                                                                          

?>

相关文章

网友评论

      本文标题:traits vs inheritance is that me

      本文链接:https://www.haomeiwen.com/subject/ltofottx.html