note 126442 added to language.oop5.anonymous
| From: | blue_1598.yahoo.com via php-notes | Date: | Tue, 21 Sep 2021 13:21:42 +0000 |
| Subject: | note 126442 added to language.oop5.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216981@lists.php.net to get a copy of this message | ||
**** 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/>";
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 92.84.242.69)
----
Manual Page -- https://php.net/manual/en/language.oop5.anonymous.php
Edit -- https://main.php.net/note/edit/126442
Del: integrated -- https://main.php.net/note/delete/126442/integrated
Del: useless -- https://main.php.net/note/delete/126442/useless
Del: bad code -- https://main.php.net/note/delete/126442/bad+code
Del: spam -- https://main.php.net/note/delete/126442/spam
Del: non-english -- https://main.php.net/note/delete/126442/non-english
Del: in docs -- https://main.php.net/note/delete/126442/in+docs
Del: other reasons-- https://main.php.net/note/delete/126442
Reject -- https://main.php.net/note/reject/126442
Search -- https://main.php.net/manage/user-notes.php