Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: max(eight, five)?
(Example: nine)

The Note You're Voting On

t8 at AT pobox dot com
13 years ago
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'

?>

<< Back to user notes page

To Top