RE: [PHP] How to call method from another class
| From: | Joel Boonstra | Date: | Mon, 28 Jan 2002 20:07:50 +0000 |
| Subject: | RE: [PHP] How to call method from another class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-82464@lists.php.net to get a copy of this message | ||
> first create an instance of class B like this...
>
> $test = new classB();
>
> Now, u can refer to any function or method of classB like this:
>
> $test->hello(); // where hello() is a method of classB..
That's not what the original question was, though. David (I think, unless
I got my nested replies wrong), was looking to call a method of ClassA
with an object of ClassB:
> > If I have a PHP class (let's say it's called ClassA), how do I call a
> > method from another class (ClassB) within ClassA? Thanks.
The answer to this question, unless my OOP theory is gone, is that you
can't, unless ClassB is an extension of classA.
So if you have:
class ClassA {
// some vars
function get_contents() {
// some stuff
return $something;
}
}
and then:
class ClassB extends ClassA {
// some vars
function display_contents() {
print $this->get_contents();
}
}
Then you're good. Otherwise, I don't think it's possible...
--
[ joel boonstra | jboonstra@gospelcom.net ]