note 90474 deleted from language.oop5.basic by cmb

From: Date: Mon, 21 Nov 2016 18:12:48 +0000
Subject: note 90474 deleted from language.oop5.basic by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207266@lists.php.net to get a copy of this message
Note Submitter: ialsoagree ---- Unfortunately, Arpit's solution creates a new class and leaves the old class inaccessible. If you need access to members of the class you are in you'll be unable to get such access. This can be a huge problem. However, there is a solution: <?php class MyClass { public $message = 'Hello'; public function MyClassFunction() { function innerFunction(&$this_thing, $message = null) { $this_thing->message = (!is_null($message)) ? $message : $this_thing->message; $this_thing->echo_something(); } innerFunction($this); // echoes 'Hello' innerFunction($this, '<br/>New Message'); // echoes '<br/>New Message' } public function echo_something() { echo $this->message; } } $class = new MyClass; $class->MyClassFunction(); ?> By passing $this as a variable by reference, you can access members of the class and even update them. If you don't want to be able to update them, you can simply pass $this to the function but not as a reference.

« previous php.notes (#207266) next »