You are here: Re: Defining a callback Func for preg_replace_callback(), within some class's method « PHP Programming Language « IT news, forums, messages
Re: Defining a callback Func for preg_replace_callback(), within some class's method

Posted by d on 09/26/24 11:37

<jody.florian@gmail.com> wrote in message
news:1137450403.012498.14620@g43g2000cwa.googlegroups.com...
> Hi,
>
> I'm trying to use preg_replace_callback within a method. The
> preg_replace_callback() & mycallback() pair will only be used by this
> method, and this method will probably only be called once in the
> object's lifetime (so there's no sense in making the callback function
> a method of the class...(is there??))
>
> Instead, I wanted to use create_function() to create a make-shift
> callback function within the method. (seeing as you can't define a
> function within a method). However, it needs to be able to access a
> property (or its getter) of the object, and it complains that it's not
> within the right scope to access it.
>
> What's the best way of doing this?
>
> class myclass {
> ...
> function mycallback($matches){
> return $this->properties[$matches[1]]
> }
> function mymethod ($subject){
> $mystr = preg_replace_callback(
> "!{([a-z]+)}!",
> array($this,'mycallback'),
> $subject
> )
> }
> ...
> }

Have you tried using the create_function property? If you read the
documentation page for preg_replace_callback (*ahem*), you'll see an example
:)

http://uk.php.net/manual/en/function.preg-replace-callback.php

> This above is the only way I've found of making this work so far.
> However, mycallback() I really don't think should have the scope it
> does, i would like its definition to stay constrained to mymethod().
> any ideas would be greatly appreaciated!
>
> Jody
>

 

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

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация