note 126442 deleted from language.oop5.anonymous by salathe

From: Date: Tue, 21 Sep 2021 18:21:57 +0000
Subject: note 126442 deleted from language.oop5.anonymous by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216985@lists.php.net to get a copy of this message
Note Submitter: blue_1598 at yahoo dot com ---- **** TOPIC: About the INNER CLASS that EXTEND the OUTHER CLASS:: **** The behaviour isn't explained in detail in the PHP manual at all! ** OBSERVATION 1 : The anonymous inner class it's like a fully independent class that at instantiation automatically clone (copy throught assignement inner_prop = outer_prop) the values of old outer class instance properties (public properties are always copied and also protected properties copied only if "INNER class extends OUTER class"). ** OBSERVATION (2)-> BE CAREFUL HERE I have encountered 1 (one) strange nuts behavior: Its like protected and public inherited methods but not overrided have their own private properties idependent from both classes simultaniously (from outer class and even from inner class). ** THE SOURCE CODE IS: ** <?php class Outer { private $private = 'private'; protected $protected = 0; public $public = 0; protected $protected_as_arg = "protected_as_arg_SEND"; public function getInfo() : string{ return "PROPERTIES : private => $this->private ;; protected_as_arg => $this->protected_as_arg ;; protected => $this->protected ;; public => $this->public<br>"; } public function incrementAll() : void{ $this->protected++; $this->public++; } public function tryToForceTheChangeOf_private_Property_From_Inner_Class_After_Inheritance() : void{ $this->private = 'SUCCESS_Forced_Change_Of_private_by_inherited_public_method'; } public function func2() { return new class($this->private, $this->protected_as_arg) extends Outer { private $private = 'nothing'; protected $protected_as_arg = 'protected_as_arg_RECEIVED'; public function __construct($private, $protected_as_arg) { $this->private = $private; $this->protected_as_arg = $protected_as_arg; echo "__construct(\$private) = $private<br>"; } public function incrementAll() : void{ //call of public method that alter private member //the method is inherited and isn't overrided at all $this->tryToForceTheChangeOf_private_Property_From_Inner_Class_After_Inheritance(); $this->protected += 22; $this->public += 44; //here directly from INNER method implementation //try override the self private Property, //this works only for the self, own, overrided implementations // but in the protected inherited "function getInfo()" call that isn't overrided the change isn't visible at all $this->private = 'private_INNER_Updated'; //for protected works change works fine. Just private members have this issue! $this->protected_as_arg = 'protected_INNER_Updated'; } function __toString(): string { return " @@ inner_ANONYMOUS : " . "private => $this->private ;; protected_as_arg => $this->protected_as_arg ;; <br> PROTECTED CALL OF getInfo() {". $this->getInfo() . "}<br>"; } }; } function __toString(): string { return " @ outer : " . $this->getInfo() . ""; } } $outer = new Outer(); $inner= $outer->func2(); echo "<br/>INITIAL VALUES :{<br/>"; echo $outer; echo $inner; echo "}<br/>"; echo "<br/>PROCESSING :{<br/><br/>"; for($i = 1; $i <= 2; $i++) { echo "AT STEP (-- $i --) :<br/>"; $outer->incrementAll(); echo $outer; $inner->incrementAll(); echo $inner; echo "<br/>"; } echo "}<br/>"; ?>

« previous php.notes (#216985) next »