PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > return function in class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
return function in class

Réponse
 
LinkBack Outils de la discussion
Vieux 18/06/2008, 22h07   #1
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut return function in class

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
  Réponse avec citation
Vieux 18/06/2008, 22h27   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return function in class

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
  Réponse avec citation
Vieux 19/06/2008, 03h08   #3
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: return function in class

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();
> ?>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10989 seconds with 11 queries