note 90072 deleted from language.oop5.basic by danbrown
| From: | danbrown@php.net | Date: | Sat, 04 Apr 2009 22:18:15 +0000 |
| Subject: | note 90072 deleted from language.oop5.basic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152820@lists.php.net to get a copy of this message | ||
Note Submitter: ialsoagree
----
@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'
?>