note 125386 added to language.oop5.traits
| From: | guidobelluomo at gmail dot com | Date: | Tue, 22 Sep 2020 06:57:41 +0000 |
| Subject: | note 125386 added to language.oop5.traits | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215455@lists.php.net to get a copy of this message | ||
If you override a method which was defined by a trait, calling the parent method will also call the
trait's override. Therefore if you need to derive from a class which has a trait, you can
extend the class without losing the trait's functionality:
<?php
trait ExampleTrait
{
public function output()
{
parent::output();
echo "bar<br>";
}
}
class Foo
{
public function output()
{
echo "foo<br>";
}
}
class FooBar extends Foo
{
use ExampleTrait;
}
class FooBarBaz extends FooBar
{
use ExampleTrait;
public function output()
{
parent::output();
echo "baz";
}
}
(new FooBarBaz())->output();
?>
Output:
foo
bar
baz
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.172 (proxied: 176.200.211.163)
----
Manual Page -- http://php.net/manual/en/language.oop5.traits.php
Edit -- https://master.php.net/note/edit/125386
Del: integrated -- https://master.php.net/note/delete/125386/integrated
Del: useless -- https://master.php.net/note/delete/125386/useless
Del: bad code -- https://master.php.net/note/delete/125386/bad+code
Del: spam -- https://master.php.net/note/delete/125386/spam
Del: non-english -- https://master.php.net/note/delete/125386/non-english
Del: in docs -- https://master.php.net/note/delete/125386/in+docs
Del: other reasons-- https://master.php.net/note/delete/125386
Reject -- https://master.php.net/note/reject/125386
Search -- https://master.php.net/manage/user-notes.php