The example comes from the book “Professional PHP5” by Edward Lecky-Thompson.
function __get($propertyName) {
if(!array_key_exists($propertyName, $this->propertyTable))
throw new Exception("Błędna własność \"$propertyName\"!");
if(method_exists($this, 'get' . $propertyName)) {
return call_user_func(array($this, 'get' . $propertyName));
} else {
return $this->data[$this->propertyTable[$propertyName]];
}
}
Can somebody please explain what happens exactly step-by-step in the call_user_func function?
On php.net I’ve read that the first parameter is a function to be called and the remaining parameters are passed to that function as its parameters.
On php.net there were simple examples and I had no problem understanding them. However I don’t get it why in the above example there is an array, and why $this as the first element of the array?
P.S.
I found a similar question on stackoverflow, and I understand what the code is supposed to do but I don’t understand fully why what’s written works.
Here’s the link to the similar question:
PropertyObject
The two-part array is PHP’s informal formal convention of passing the method of a specific object as a callback/callable. See http://php.net/manual/en/language.types.callable.php for details.
array($this, 'getFoo')simply stands for thegetFoomethod of the$thisobject.