|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I had this little bit of code:
function parseTemplateData($file){ $file2=preg_replace_callback( "/<edit(.*?)>/s", "parseEditTag", $file); echo $file2; } and that did what I needed, so I thought I'll put it in a class: class FOO{ public function parseTemplateData($file){ $file2=preg_replace_callback( "/<edit(.*?)>/s", "parseEditTag", $file); echo $file2; } public function parseEditTag($tag_content){ .... } It is, of course looking for parseEditTag *outside* the class. So, I thought: $file2=preg_replace_callback( "/<edit(.*?)>/s", "$this->parseEditTag", $file); Which failed. Then I thought: $file2=preg_replace_callback( "/<edit(.*?)>/s", EDIT_PAGE::parseEditTag(), $file); Now that calls the function, but doesn't pass in the match. So I thought I'd try to use create_function. But even if I could figure out how to pass in the matches, I still can't get it to call the method in the class. So, I'm thinking there must be another plan. Or another beer. Maybe I'm just thinking too perl! Jeff |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Wed, 18 Jun 2008 23:07:40 +0200, Jeff <jeff@spam_me_not.com> wrote:
> I had this little bit of code: > > function parseTemplateData($file){ > $file2=preg_replace_callback( > "/<edit(.*?)>/s", > "parseEditTag", > $file); > > > echo $file2; > } > > and that did what I needed, so I thought I'll put it in a class: > > class FOO{ > > public function parseTemplateData($file){ > $file2=preg_replace_callback( > "/<edit(.*?)>/s", > "parseEditTag", > $file); > > > echo $file2; > } > > public function parseEditTag($tag_content){ > ... > > } Valid callbacks are: $callback = 'function_name'; $callback = array($instanceofObject,'method_name'); $callback = array('ClassName','static_method_name')); If you went to the manual for preg_replace_callback: http://nl2.php.net/preg_replace_callback ...and clicked on 'callback', you end up here: http://nl2.php.net/manual/en/languag...ypes..callback <?php class Foo{ function test(){ echo preg_replace_callback('/a/',array($this,'funcname'),"acac"); echo preg_replace_callback('/a/',array('Bar','funcname'),"acac"); echo preg_replace_callback('/a/','funcname',"acacacaca"); } function funcname(){ return 'b'; } } class Bar{ static function funcname(){ return 'd'; } } function funcname(){ return 'e'; } $f = new Foo(); $f->test(); ?> -- Rik Wasmus ....spamrun finished |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Rik Wasmus wrote:
> On Wed, 18 Jun 2008 23:07:40 +0200, Jeff <jeff@spam_me_not.com> wrote: > >> I had this little bit of code: >> >> function parseTemplateData($file){ >> $file2=preg_replace_callback( >> "/<edit(.*?)>/s", >> "parseEditTag", >> $file); >> >> >> echo $file2; >> } >> >> and that did what I needed, so I thought I'll put it in a class: >> >> class FOO{ >> >> public function parseTemplateData($file){ >> $file2=preg_replace_callback( >> "/<edit(.*?)>/s", >> "parseEditTag", >> $file); >> >> >> echo $file2; >> } >> >> public function parseEditTag($tag_content){ >> ... >> >> } > > Valid callbacks are: > $callback = 'function_name'; > $callback = array($instanceofObject,'method_name'); > $callback = array('ClassName','static_method_name')); > > If you went to the manual for preg_replace_callback: > http://nl2.php.net/preg_replace_callback > ..and clicked on 'callback', you end up here: > > http://nl2.php.net/manual/en/languag...types.callback Thanks for the following example, it's perfectly clear. And since the next thing on my list to learn was "call_user_func", I've already learned that with the 'callback' lesson! Jeff > > > <?php > class Foo{ > function test(){ > echo preg_replace_callback('/a/',array($this,'funcname'),"acac"); > echo preg_replace_callback('/a/',array('Bar','funcname'),"acac"); > echo preg_replace_callback('/a/','funcname',"acacacaca"); > } > function funcname(){ > return 'b'; > } > } > class Bar{ > static function funcname(){ > return 'd'; > } > } > function funcname(){ > return 'e'; > } > $f = new Foo(); > $f->test(); > ?> |
|
![]() |
| Outils de la discussion | |
|
|