note 90474 added to language.oop5.basic

From: Date: Fri, 24 Apr 2009 03:03:30 +0000
Subject: note 90474 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-153644@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 76.78.201.63 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/90474 Del: integrated -- https://master.php.net/note/delete/90474/integrated Del: useless -- https://master.php.net/note/delete/90474/useless Del: bad code -- https://master.php.net/note/delete/90474/bad+code Del: spam -- https://master.php.net/note/delete/90474/spam Del: non-english -- https://master.php.net/note/delete/90474/non-english Del: in docs -- https://master.php.net/note/delete/90474/in+docs Del: other reasons-- https://master.php.net/note/delete/90474 Reject -- https://master.php.net/note/reject/90474 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153644) next »