PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Invoking static class functions within call_user_functions
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Invoking static class functions within call_user_functions

Réponse
 
LinkBack Outils de la discussion
Vieux 25/10/2007, 19h02   #1
Craig Taylor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Invoking static class functions within call_user_functions

Apparently there is no 'good' way to invoke static class functions
from within the call_user_function in PHP 5 (I'm using PHP 5 1.2).

Example:
------------
<?php
// Does php support something akin to C style function pointers??
//

class testClass
{
static function myFunction( $parm1 )
{
echo 'Hello, parm1 = '.$parm1."\n";
}
}

$className = 'testClass';
$fnName= 'myFunction';

// Are these legal?
//($fnPtr)('abc'); ... NOT LEGAL
//$fnPtr('abc'); ... NOT LEGAL

// call_user_func( $className, $fnPtr, 'abc' );

// Gah... need to instantiate static class members...
$x = new $className();
$x->{$fnName}('abc');
?>

----

There is documentation on php.net for call_user_func on how to invoke
it with a non-static class member, eg: call_user_func( 'classname',
'fnName', 'param1'... ).

The way that I've got it working feels like a kludge to me as I'm
having to instantiate an object that is never going to be used. (I'm
using static functions within the class as er functions for my
application's data classes).

Thanks,

- Craig Taylor

  Réponse avec citation
Vieux 25/10/2007, 20h20   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Invoking static class functions within call_user_functions

On Oct 25, 2:02 pm, Craig Taylor <ctalk...@gmail.com> wrote:
> Apparently there is no 'good' way to invoke static class functions
> from within the call_user_function in PHP 5 (I'm using PHP 5 1.2).
>
> Example:
> ------------
> <?php
> // Does php support something akin to C style function pointers??
> //
>
> class testClass
> {
> static function myFunction( $parm1 )
> {
> echo 'Hello, parm1 = '.$parm1."\n";
> }
>
> }
>
> $className = 'testClass';
> $fnName= 'myFunction';
>
> // Are these legal?
> //($fnPtr)('abc'); ... NOT LEGAL
> //$fnPtr('abc'); ... NOT LEGAL
>
> // call_user_func( $className, $fnPtr, 'abc' );
>
> // Gah... need to instantiate static class members...
> $x = new $className();
> $x->{$fnName}('abc');
> ?>
>
> ----
>
> There is documentation on php.net for call_user_func on how to invoke
> it with a non-static class member, eg: call_user_func( 'classname',
> 'fnName', 'param1'... ).
>
> The way that I've got it working feels like a kludge to me as I'm
> having to instantiate an object that is never going to be used. (I'm
> using static functions within the class as er functions for my
> application's data classes).
>
> Thanks,
>
> - Craig Taylor



Call a class (static) method:

call_user_func(array('testClass', 'myFunction'), $arg1, $arg2, $arg3);

Call an instance method:

$t = new testClass();
call_user_func(array($t, 'myFunction'), $arg1, $arg2, $arg3);

  Réponse avec citation
Vieux 25/10/2007, 20h47   #3
Craig Taylor
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Invoking static class functions within call_user_functions

Just what I was looking for...

When I had looked on php.net/call_user_func for this situation I saw
the example and misread it as not having the arrays - rechecked after
your post and my eyes are seeing it now... (that or the evil gremlin
that sneaks in my code is now messing with other people's sites
too... )

Thanks,


On Oct 25, 3:20 pm, ZeldorBlat <zeldorb...@gmail.com> wrote:
> On Oct 25, 2:02 pm, Craig Taylor <ctalk...@gmail.com> wrote:
>
>
>
> > Apparently there is no 'good' way to invoke static class functions
> > from within the call_user_function in PHP 5 (I'm using PHP 5 1.2).

>
> > Example:
> > ------------
> > <?php
> > // Does php support something akin to C style function pointers??
> > //

>
> > class testClass
> > {
> > static function myFunction( $parm1 )
> > {
> > echo 'Hello, parm1 = '.$parm1."\n";
> > }

>
> > }

>
> > $className = 'testClass';
> > $fnName= 'myFunction';

>
> > // Are these legal?
> > //($fnPtr)('abc'); ... NOT LEGAL
> > //$fnPtr('abc'); ... NOT LEGAL

>
> > // call_user_func( $className, $fnPtr, 'abc' );

>
> > // Gah... need to instantiate static class members...
> > $x = new $className();
> > $x->{$fnName}('abc');
> > ?>

>
> > ----

>
> > There is documentation on php.net for call_user_func on how to invoke
> > it with a non-static class member, eg: call_user_func( 'classname',
> > 'fnName', 'param1'... ).

>
> > The way that I've got it working feels like a kludge to me as I'm
> > having to instantiate an object that is never going to be used. (I'm
> > using static functions within the class as er functions for my
> > application's data classes).

>
> > Thanks,

>
> > - Craig Taylor

>
> Call a class (static) method:
>
> call_user_func(array('testClass', 'myFunction'), $arg1, $arg2, $arg3);
>
> Call an instance method:
>
> $t = new testClass();
> call_user_func(array($t, 'myFunction'), $arg1, $arg2, $arg3);



  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 09h13.


É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,09290 seconds with 11 queries