note 124149 added to language.oop5.cloning

From: Date: Wed, 28 Aug 2019 07:53:57 +0000
Subject: note 124149 added to language.oop5.cloning
Groups: php.notes 
Request: Send a blank email to php-notes+get-212029@lists.php.net to get a copy of this message
Here is a basic example about clone issue. If we use clone in getClassB method. Return value will be same as new B() result. But it we dont use clone we can effect B::$varA. class A { protected $classB; public function __construct(){ $this->classB = new B(); } public function getClassB() { return clone $this->classB; } } class B { protected $varA = 2; public function getVarA() { return $this->varA; } public function setVarA() { $this->varA = 3; } } $a = new A(); $classB = $a->getClassB(); $classB->setVarA(); echo $a->getClassB()->getVarA() . PHP_EOL;// with clone -> 2, without clone it returns -> 3 echo $classB->getVarA() . PHP_EOL; // returns always 3 ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.168 (proxied: 85.99.105.75) ---- Manual Page -- http://php.net/manual/en/language.oop5.cloning.php Edit -- https://master.php.net/note/edit/124149 Del: integrated -- https://master.php.net/note/delete/124149/integrated Del: useless -- https://master.php.net/note/delete/124149/useless Del: bad code -- https://master.php.net/note/delete/124149/bad+code Del: spam -- https://master.php.net/note/delete/124149/spam Del: non-english -- https://master.php.net/note/delete/124149/non-english Del: in docs -- https://master.php.net/note/delete/124149/in+docs Del: other reasons-- https://master.php.net/note/delete/124149 Reject -- https://master.php.net/note/reject/124149 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#212029) next »