Passing $this to function

oop, php

Solution

From the PHP manual for Callbacks:

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

Example below:

// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));

Problem

In Javascript there is `call()` and `apply()`, that partially, but resolves to `call_user_func()` and `call_user_func_array()` in PHP. Now, the difference here is that we can pass a variable with `call()` and `apply()` to be used as `this` within the function scope. Can I achieve something like this with PHP? Update: In Javascript: ``` var x = function(passed) { return { dis : this, passd : passed }; }; console.log(x(44)); // window, 44 console.log(x.call(25, 44)); // 25, 44 ``` `.call()` first parameter within the function scope, becomes `this`.

Original source