|
Posted by Kimmo Laine on 10/10/06 08:59
"Colin Fine" <news@kindness.demon.co.uk> wrote in message
news:egehjm$de9$1$8302bc10@news.demon.co.uk...
> Kimmo Laine wrote:
>> I tried to use a class member function as a callback in
>> preg_replace_callback, but failed. It announces: " Warning:
>> preg_replace_callback() [function.preg-replace-callback]: requires
>> argument 2, 'myclass::myfunction', to be a valid callback in ..."
>>
>> I tried both myclass::myfunction and $this->myfunction, neither worked.
>> Is there really no way of using a class member as a callback, it has to
>> be an external function? That sucks... :(
>>
>>
>>
> From
> http://uk.php.net/manual/en/language.pseudo-types.php#language.types.callback:
>
> "Some functions like call_user_func() or usort() accept user defined
> callback functions as a parameter. Callback functions can not only be
> simple functions but also object methods including static class methods.
>
> "A method of an instantiated object is passed as an array containing an
> object as the element with index 0 and a method name as the element with
> index 1.
>
> "Static class methods can also be passed without instantiating an object
> of that class by passing the class name instead of an object as the
> element with index 0. "
>
Thanks Colin!
Altough it turned out I don't actually need this after all I found a nicer
solution, it's good to know it works. I did try it and this works just
perfectly:
<?php
class Foo {
public function __construct(){
echo preg_replace_callback(
'/./', array(get_class($this), 'bar'), 'lorem ipsum' );
}
private function bar($matches){
return strtoupper($matches[0]);
}
}
$my_foo = new Foo();
?>
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti pδivittyvδ nettisarjis
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)
[Back to original message]
|