0

I have a document that is able to use $this to access certain bits of info. Inside that document there is a function which cannot access $this (gives an error about not being inside an object).

Is there any way to allow it access to $this, so I can run the class's methods from within the function?

I have tried using globals but to no avail.

2
  • 1
    Pass it to the function as an argument. Commented Apr 18, 2011 at 13:03
  • Of course, no idea why I didn't realise. Cheers! Commented Apr 18, 2011 at 13:08

3 Answers 3

3

If the function is defined outside the class, you can pass in an instance of the object and use that.

For example:

class A
{
    public function B()
    {
        echo "C";
    }
}

function D($obj)
{
    echo $obj->B();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's perfect. Should have realised that myself though. (Late night, I suppose!)
Will do.. but I have to wait a few minutes apparently :)
0

Rewrite the function so that it expects an additional parameter. Pass $this as argument to that function

Comments

0

Assuming that the function is part of the class that you are trying to get a property of use self where you would have used this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.