In Javascript, you can control what object the this keyword referes to by using apply. In the following example, foo will be called with this equal to bar:
function foo() { console.log(this); }
let bar = {};
foo.apply(bar);
Is there any way to achieve the same effect in PHP? Just creating a function referencing $this outside of a class seems possible, but it is (unsurprisingly) just treated like any undefined variable.
I am aware of call_user_func_array, but it does not let me assign to $this. I am thinking reflection might be useful here, but I am not sure how.