PHP access class inside another class
Posted
by arxanas
on Stack Overflow
See other posts from Stack Overflow
or by arxanas
Published on 2010-05-19T02:02:12Z
Indexed on
2010/05/19
2:10 UTC
Read the original article
Hit count: 260
So I have two classes like this:
class foo {
/* code here */
}
$foo = new foo();
class bar {
global $foo;
public function bar () {
echo $foo->something();
}
}
I want to access the methods of foo inside all methods bar, without declaring it in each method inside bar, like this:
class bar {
public function bar () {
global $foo;
echo $foo->something();
}
public function barMethod () {
global $foo;
echo $foo->somethingElse();
}
/* etc */
}
I don't want to extend it, either. I tried using the var keyword, but it didn't seem to work. What do I do in order to access the other class "foo" inside all methods of bar?
© Stack Overflow or respective owner