Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update traits.xml Mention the hierarchy of classes, amend final modifier example #4188

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
53 changes: 37 additions & 16 deletions language/oop5/traits.xml
Original file line number Diff line number Diff line change
Expand Up @@ -425,23 +425,29 @@ Example::doSomething();
<title>Static Properties</title>
<caution>
<simpara>
Prior to PHP 8.3.0, static properties were shared across all classes
using the trait. As of PHP 8.3.0, each class using the trait has its
own copy of the static property.
Prior to PHP 8.3.0, static properties defined in a trait were shared across a single hierarchy of classes
which use the trait. As of PHP 8.3.0, each class using the trait has its
own copy of the static property if it explicitly uses the trait,
i.e. the static property defined in a trait inserted into a child class override
a static property that the child class inherited from the parent class.
Comment on lines +430 to +432
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a non-native speaker, I struggle a bit to understand the sentence. Maybe it could be cut in two?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, a native English speaker (not me) will formulate it better, or changed it to single sentence at all, something like that:

As of PHP 8.3.0, the static property of the trait inserted in the child class overrides the static property that the child class inherited from the parent class defined in conjunction with the same trait

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to me, yes!

</simpara>
</caution>
<programlisting role="php">
<![CDATA[
<?php
trait StaticExample {

trait StaticExample
{
public static $static = 'foo';
}

class Example {
class Example
{
use StaticExample;
}

echo Example::$static;

?>
]]>
</programlisting>
Expand All @@ -458,16 +464,20 @@ echo Example::$static;
<programlisting role="php">
<![CDATA[
<?php
trait PropertiesTrait {

trait PropertiesTrait
{
public $x = 1;
}

class PropertiesExample {
class PropertiesExample
{
use PropertiesTrait;
}

$example = new PropertiesExample;
$example = new PropertiesExample();
$example->x;

?>
]]>
</programlisting>
Expand Down Expand Up @@ -556,26 +566,37 @@ class ConstantsExample {
<title>Final methods</title>
<simpara>
As of PHP 8.3.0, the <link linkend="language.oop5.final">final</link>
modifier can be applied to methods coming from traits.
modifier can be applied to methods imported from traits by using the <literal>as</literal> operator,
which permits modifying the method visibility originating from a trait in the class the trait is used in.
</simpara>
<example xml:id="language.oop5.traits.final-methods.example">
<title>Defining a method coming from a trait as <literal>final</literal></title>
<programlisting role="php">
<![CDATA[
<?php
trait ConstantsTrait {
public function method() {

trait CommonTrait
{
public function method()
{
echo 'Hello';
}
}

class ConstantsExample {
use ConstantsTrait;

final public function method() {
echo 'Hello World';
class FinalExampleA
{
use CommonTrait {
CommonTrait::method as final; // The 'final' will prevents child classes
// from overriding a method
}
}

class FinalExampleB extends FinalExampleA
{
public function method() {} // Fatal error: Cannot override final method
// FinalExampleA::method()
}

?>
]]>
</programlisting>
Expand Down