Trait properties

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@319725 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Jakub Vrana 2011-11-23 20:44:23 +00:00
parent 6444355892
commit 6ee44cfc78

View file

@ -406,6 +406,57 @@ class Example {
Example::doSomething();
?>
]]>
</programlisting>
</example>
</sect2>
<sect2 xml:id="language.oop5.traits.properties">
<title>Properties</title>
<para>
Traits can also define properties.
</para>
<example xml:id="language.oop5.traits.properties.example">
<title>Defining Properties</title>
<programlisting role="php">
<![CDATA[
<?php
trait PropertiesTrait {
public $x = 1;
}
class PropertiesExample {
use PropertiesTrait;
}
$example = new PropertiesExample;
$example->x;
?>
]]>
</programlisting>
</example>
<para>
If a trait defines a property then a class can not define a property with
the same name, otherwise an error is issued. It is an
<constant>E_STRICT</constant> if the class definition is compatible (same
visibility and initial value) or fatal error otherwise.
</para>
<example xml:id="language.oop5.traits.properties.conflicts">
<title>Conflict Resolution</title>
<programlisting role="php">
<![CDATA[
<?php
trait PropertiesTrait {
public $same = true;
public $different = false;
}
class PropertiesExample {
use PropertiesTrait;
public $same = true; // Strict Standards
public $different = true; // Fatal error
}
?>
]]>
</programlisting>
</example>