note 111608 added to language.oop5.traits

From: Date: Thu, 07 Mar 2013 18:51:19 +0000
Subject: note 111608 added to language.oop5.traits
Groups: php.notes 
Request: Send a blank email to php-notes+get-193983@lists.php.net to get a copy of this message
Hi! Just a quick solution to use class-identical static property. I use it to store SQL descriptions, which is always same for the same class, but may change for an extended class. <?php interface hasO{ // just for forcing basic functionalities static function O($use_as); function myObj($append = "nothing"); function inc(); static function getName(); } class O{ // the target class to access var $val, $num = 0; function setVAl($val){$this->val = $val;} function inc(){$this->num++;} function prt($append){echo "hello, i'm {$this->val} with number: {$this->num} and append a string: {$append}<br>"; } } trait T{ // the trait to access O private static $objs = []; private static $preference = []; static function setPreference($class){ self::$preference[get_called_class()] = $class; // we can hack even polymorphism } static function O($use_as = null){ /* we use $use_as, preference or the current calling class in this order. */ $cc = ($use_as ? $use_as : (($cc2 = @self::$preference[get_called_class()])!= null ? $cc2 : get_called_class())); if (!@self::$objs[$cc]){ // you have to use 'self::' to access a private method. self::$objs[$cc] = new O(); self::$objs[$cc]->setVal(static::getName()); // but you can use 'static::' to access class method. } return self::$objs[$cc]; } } class A implements hasO{ // A is independent from the other classes use T; static function getName() {return 'A';} function myObj($append = "i'm A",$use_as = null){ if (!$append && $append !== false) $append = "i'm ".get_called_class(); self::O($use_as)->prt($append); } function inc(){self::O()->inc();} } class B implements hasO{ use T; function myObj($append = null){ if (!$append && $append !== false) $append = "i'm ".get_called_class(); static::O()->prt($append); } function inc(){static::O()->inc();} static function getName() {return get_called_class();} } class C extends B{} // not relevant inheritence. class D extends B{ use T; // you won't get errors if you accidently reuse the same trait static function getName() {return 'a child of B ';} // we can use current object static methods, so inheritence is intact function inc(){ static::O()->inc(); // both self::O and static::O is usable to preserve further inherits self::O()->inc(); } } class E extends B{ const CLSS = 'E'; function myObj($append="EEEE!", $use_as = null){ if (!$append && $append !== false) $append = "i'm ".get_called_class(); self::O($use_as)->prt($append); } } $a = new A;$b = new B;$c = new C;$d = new D;$e = new E; // create all objects... $b->myObj(); $b->inc(); $b->myObj(); $c->myObj(); $c->inc(); $c->myObj(); // C comes before A, so C's object is already instantized before A tries to access it $a->myObj(); $a->inc(); $a->myObj(); $a->myObj(null,'C'); // this isn't works, because it's protected private $d->myObj(); $d->inc(); $d->myObj(); $e->myObj(); $e->inc(); $e->myObj("i'm E again"); $e->myObj(0,'C'); // but this IS working, even if it's private NOT protected. $e::setPreference('B'); // we want to morph to B $e->myObj(); $e->inc(); // we incement B's object now as seen bellow $e->myObj(); $b->myObj(); ?> ---- Server IP: 69.147.83.199 Probable Submitter: 88.65.217.5 ---- Manual Page -- http://php.net/manual/en/language.oop5.traits.php Edit -- https://master.php.net/note/edit/111608 Del: integrated -- https://master.php.net/note/delete/111608/integrated Del: useless -- https://master.php.net/note/delete/111608/useless Del: bad code -- https://master.php.net/note/delete/111608/bad+code Del: spam -- https://master.php.net/note/delete/111608/spam Del: non-english -- https://master.php.net/note/delete/111608/non-english Del: in docs -- https://master.php.net/note/delete/111608/in+docs Del: other reasons-- https://master.php.net/note/delete/111608 Reject -- https://master.php.net/note/reject/111608 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193983) next »