note 113905 deleted from language.oop5.traits by cmb
| From: | cmb@php.net | Date: | Tue, 08 Sep 2015 19:43:56 +0000 |
| Subject: | note 113905 deleted from language.oop5.traits by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203738@lists.php.net to get a copy of this message | ||
Note Submitter: majoro chitetskoy at gmail dot com
----
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.