note 90072 added to language.oop5.basic
| From: | ialsoagree at osu1 dot php dot net | Date: | Sat, 04 Apr 2009 22:14:35 +0000 |
| Subject: | note 90072 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152819@lists.php.net to get a copy of this message | ||
@Arpit
That's a great solution, that's for pointing it out.
The original purpose of my message, however, was to point out that you wouldn't be able to
access a class's methods or members from a function defined within a method. Creating a new
object won't be useful if there's specific details in the current object you need to
obtain.
For example, if the "$message" variable had been changed and the new message was to be
echoed, creating a new object wouldn't give you access to the message you need to echo.
To make better OO code I had designed a validate class with a method that used the same code
repeatedly. This code was placed in a function that could be called within the method but I ran into
the trouble of not being able to refer to the object's own members or methods despite still
being within the object.
However, I did find a more effective solution:
<?php
class MyClass {
public $message = 'Hello';
public function MyClassFunction() {
function innerFunction($this_thing) {
$this_thing->echo_something();
}
innerFunction($this);
}
public function echo_something() {
echo $this->message;
}
}
$class = new MyClass;
$class->message = 'FooBar';
$class->MyClassFunction(); // Echoes 'FooBar'
?>
----
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/90072
Del: integrated -- https://master.php.net/note/delete/90072/integrated
Del: useless -- https://master.php.net/note/delete/90072/useless
Del: bad code -- https://master.php.net/note/delete/90072/bad+code
Del: spam -- https://master.php.net/note/delete/90072/spam
Del: non-english -- https://master.php.net/note/delete/90072/non-english
Del: in docs -- https://master.php.net/note/delete/90072/in+docs
Del: other reasons-- https://master.php.net/note/delete/90072
Reject -- https://master.php.net/note/reject/90072
Search -- https://master.php.net/manage/user-notes.php