mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Trait properties
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@319725 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
6444355892
commit
6ee44cfc78
1 changed files with 51 additions and 0 deletions
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue