You are here: Re: preg_replace_callback, class method? « PHP Programming Language « IT news, forums, messages
Re: preg_replace_callback, class method?

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)

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация