note 113905 added to language.oop5.traits

From: Date: Mon, 16 Dec 2013 08:03:48 +0000
Subject: note 113905 added to language.oop5.traits
Groups: php.notes 
Request: Send a blank email to php-notes+get-197947@lists.php.net to get a copy of this message
Some extreme Trait testing, which anyone can find useful. This involves what can be called as "Trait Inheritance" though I doubt if that is a proper word. This shows how to have some sort of inheritance in Traits. Snippet from one complex Tristan Engine code. <?php /** * Trait and Helper Backbone class */ trait Foo_HlpBbT { function mnInfo_gst($x=null, $c=false){ echo "Foo_HlpBbT has been called\n"; } } /** * Base Archvie */ class Foo_BaseArchive { use Foo_HlpBbT; // accessor properties public $_mnInfo; } /** * Testing Archive */ class Foo_ItemArchvie extends Foo_BaseArchive { } /** * Testing Archive 2 */ /** * The base handler for sub base handler */ trait Foo_Item_HlpBbT { use Foo_HlpBbT; function mnInfo_gst($x=null, $c=false){ parent::mnInfo_gst($x,$c); echo "Foo_Item_HlpBbT is called.\n"; } } /** * The base handler for sub base handler */ trait Foo_Item_HlpBbT2 { use Foo_Item_HlpBbT { Foo_Item_HlpBbT::mnInfo_gst as _e1_mnInfo_gst; } // We need that the Foo_HlpBbT::mnInfo_gst, Foo_Item_HlpBbT::mnInfo_gst and this mnInfo_gst is called. // Troubles here, only the Foo_HlpBbT and this is called, the Foo_Item_HlpBbT is skipped. // So it's important that if necessary, the function that is intended to be "overriden" from // the trait inheritances be aliased like what we did to mnInfo_gst. function mnInfo_gst($x=null, $c=false){ $this->_e1_mnInfo_gst($x,$c); echo "Foo_Item_HlpBbT2 is called.\n"; } } /** * The base handler for sub base handler */ trait Foo_Item_HlpBbT3 { use Foo_Item_HlpBbT2 { Foo_Item_HlpBbT2::mnInfo_gst as _e2_mnInfo_gst; } // Another deeper Trait declaration function mnInfo_gst($x=null, $c=false){ $this->_e2_mnInfo_gst($x,$c); echo "Foo_Item_HlpBbT3 is called.\n"; } } /** * Item Archive */ class Foo_Item_ItemArchive extends Foo_BaseArchive { use Foo_Item_HlpBbT3; } // Testing na $tang = new Foo_Item_ItemArchive(); $tang->mnInfo_gst(); ?> outputs: Foo_HlpBbT has been called Foo_Item_HlpBbT is called. Foo_Item_HlpBbT2 is called. Foo_Item_HlpBbT3 is called. ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/language.oop5.traits.php Edit -- https://master.php.net/note/edit/113905 Del: integrated -- https://master.php.net/note/delete/113905/integrated Del: useless -- https://master.php.net/note/delete/113905/useless Del: bad code -- https://master.php.net/note/delete/113905/bad+code Del: spam -- https://master.php.net/note/delete/113905/spam Del: non-english -- https://master.php.net/note/delete/113905/non-english Del: in docs -- https://master.php.net/note/delete/113905/in+docs Del: other reasons-- https://master.php.net/note/delete/113905 Reject -- https://master.php.net/note/reject/113905 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#197947) next »