Hi...

Originally Posted by
OOPNoob
Hello,
PHP Code:
class Helpers {
public $x;
public function static p($x) {
print '<pre>';
print_r($x);
print '</pre>';
return $x;
}
public function pp($someArray) {
$xs = func_get_args();
array_map(self::p($someArray), $xs);
}
}
$helper = new Helpers();
$someArray = array("firstname"=>"Site", "lastname"=>"Point");
echo $helper->pp($someArray);
What am i doing wrong?
By default the callback is a function name. My versions above are functions, not class methods. If you have...
PHP Code:
function p($x) {
print '<pre>';
print_r($x);
print '</pre>';
return $x;
}
function pp($someArray) {
$xs = func_get_args();
array_map('p', $xs);
}
...without any classes, then all should be fine.
If you want to pass a method on an object as a callback, here is what you do...
PHP Code:
class Multiplier {
private $factor;
function __construct($factor) {
$this->factor = $factor;
}
function actOn($value) {
return $value * $this->factor;
}
}
$doubler = new Multiplier(2);
print_r(array_map(array(doubler, 'actOn'), array(1, 2, 3)));
A callback to an object is a two item array, the first item the object and the second item the function name.
If you want to call a static method...
PHP Code:
class Doubler {
static function double($value) {
return 2 * $value;
}
}
print_r(array_map(array('Doubler', 'double'), array(1, 2, 3));
Now the callback is a two item array, the first the class name and the second the static method name.
It's clumsy, and all because OO was added to the language at quite a late stage. You don't need to use array_map(), I was just trying to shorten the code a little (although array_map() is nice once you get the hang of it). array_map() is a functional construct, rather than specifically an OO one.
Frankly the examples here are a bit silly. You need something more complicated o make OO useful.
As for writing a debug class, that's probably not a good idea. It's too simple a job to require OO. Do you have a real world problem you'd like to solve. Maybe I can sugest some code for that.
yours, Marcus
Bookmarks