note 124025 added to language.oop5.traits

From: Date: Mon, 08 Jul 2019 11:34:26 +0000
Subject: note 124025 added to language.oop5.traits
Groups: php.notes 
Request: Send a blank email to php-notes+get-211540@lists.php.net to get a copy of this message
Like shown in other examples it is possible to define a constructors and destructors in traits: <? php trait T { public $prop = null; public function __construct($prop) { echo "Constructor called\n"; $this->prop = $prop; } public function __destruct() { echo "Destructor called\n"; } } class A { use T; } $a = new A("Hello World\n"); // Constructor called echo $a->prop; // Hello World // Destructor called ?> It is also possible to use trait methods as constructor/destructor using them with the alias '__construct'/'__destruct': <?php trait T { public $prop = null; public function constructor($prop) { echo "Constructor called\n"; $this->prop = $prop; } public function destructor() { echo "Destructor called\n"; } } class A { use T { T::constructor as __construct; T::destructor as __destruct; } } $a = new A("Hello World\n"); // Constructor called echo $a->prop; // Hello World // Destructor called ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.195 (proxied: 2a02:8109:dc0:a4:1967:358c:294f:6e92) ---- Manual Page -- http://php.net/manual/en/language.oop5.traits.php Edit -- https://master.php.net/note/edit/124025 Del: integrated -- https://master.php.net/note/delete/124025/integrated Del: useless -- https://master.php.net/note/delete/124025/useless Del: bad code -- https://master.php.net/note/delete/124025/bad+code Del: spam -- https://master.php.net/note/delete/124025/spam Del: non-english -- https://master.php.net/note/delete/124025/non-english Del: in docs -- https://master.php.net/note/delete/124025/in+docs Del: other reasons-- https://master.php.net/note/delete/124025 Reject -- https://master.php.net/note/reject/124025 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211540) next »