I have some code that’s run within a method (it’s a CakePHP view):
This works:
$this->foo();
This doesn’t:
function bar() {
$this->foo();
} // Using $this when not in object context
Neither does this:
function bar() {
global $this;
$this->foo();
} // Cannot re-assign $this
Nor this:
$that = $this;
$bar = function() {
global $that;
$that->foo();
} // Trying to get property of non-object
I’d like to use the object’s library function from within this method, but bar has to stay a local subprocedure (moving it to be a class method would be pointless). Any solutions or workarounds?
In PHP 5.3:
With PHP 5.4, the above hack isn’t required.