I'm trying to use my class inside a function called inside() I have something like:
class MyClass{
public function test(){ echo "test ok"; }
}
$mc = new MyClass();
function inside(){
global $mc;
$mc->test();
}
But this doesn't work :( I get:
Fatal error: Call to a member function test() on a non-object in ...
One solution would be to pass the class $mc as an argument to the function inside() but I want something else :/
Any idea ?
Ty guys
If all you want is to access a function residing inside a class you can define it as static
class MyClass {
static function test() {
echo "test ok";
}
}
Then you don't have to use a global:
function inside() {
MyClass::test();
}
You need to define test as public function test(){...
.
Since the inside
function depends on the MyClass
class, it should be passed in as a dependecy injection.
$mc = new MyClass;
function inside(MyClass $mc){
$mc->test();
}
It looks like you need a refresher of the (Gang of Fours) injection design pattern, http://martinfowler.com/articles/injection.html