note 126444 added to language.oop5.anonymous

From: Date: Tue, 21 Sep 2021 17:28:17 +0000
Subject: note 126444 added to language.oop5.anonymous
Groups: php.notes 
Request: Send a blank email to php-notes+get-216983@lists.php.net to get a copy of this message
Just a little observation. !!!! **** I've made a mistake in the OBSERVATION (1) explanation and i'll correct it here, right now! ** In the OBSERVATION (2) posted on this webpage/topic the behavior that I discovered and described it's ok explained so go to my previous message to see that strange behavior and run the code that I attached there . **** TOPIC: About the INNER CLASS that EXTEND the OUTHER CLASS:: **** ** OBSERVATION 1 CORRECTED: (The anonymous inner class access throught inheritance the visible members [public/protected] but don't modify anything because they aren't transmitted by reference what confused me before run my own tests). The anonymous inner class (that inherit the outer one) it's like a normal named class that it's a fully independent class that at instantiation automatically clone (copy throught assignement inner_prop = outer_prop) the values of old outer class Vvisible instance properties [public and protected] because of the inheritance mecanism ("INNER class extends OUTER class"). Also the visible [public/protected] methods of the outer class are inherited. :) It's like a normal class extend another class. In particular case I like to name an anonymous class : "DISPOSABLE CLASS "; intended to be used only for one unique instantiation! The only difference that apperas here is that the anonymous class hasn't a name and can be use only to instantiate 1 (one) object at a time. If u want another one must rewrite the class code again (and u want insted to create a normal class and instantiate any objects u need). P.S: The major difference that I suddenly discovered it's in case of the private members. I exposed that special behavior in my previous message at the " OBSERVATION (2) - here: " https://www.php.net/manual/en/language.oop5.anonymous.php#126442 " - go and check/search it on this webage). I think it's related to some old link created but not updated by the PHP engine for a inner variable of an self PHP engine created object so that the private property inside inherited but not overrided method points to an old container[type,value,is_ref,cnt_ref] structure (that PHP use to store and manipulate the data) and not the one of the current anonymous class object instance. Of course there are my speculation, I'm a PHP beginner programmer and I might be wrong! Also take care at this situation: If you don't extend the outer class ("INNER class extends OUTER class") even if the class is inside another method class (even inside magic methods) you will have just a simple anonymous class that behave like u code it yourself and it's shown in the first example of this manual. The first example of this manual (a sample anonymous class used to instantiate an object): <?php // Using an explicit class class Logger { public function log($msg) { echo $msg; } } $util->setLogger(new Logger()); // Using an anonymous class $util->setLogger(new class { public function log($msg) { echo $msg; } }); ---- 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/126444 Del: integrated -- https://main.php.net/note/delete/126444/integrated Del: useless -- https://main.php.net/note/delete/126444/useless Del: bad code -- https://main.php.net/note/delete/126444/bad+code Del: spam -- https://main.php.net/note/delete/126444/spam Del: non-english -- https://main.php.net/note/delete/126444/non-english Del: in docs -- https://main.php.net/note/delete/126444/in+docs Del: other reasons-- https://main.php.net/note/delete/126444 Reject -- https://main.php.net/note/reject/126444 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216983) next »