Re: Invoking static class functions within call_user_functions
- From: ZeldorBlat <zeldorblat@xxxxxxxxx>
- Date: Thu, 25 Oct 2007 19:20:26 -0000
On Oct 25, 2:02 pm, Craig Taylor <ctalk...@xxxxxxxxx> 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 helper 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);
.
- Follow-Ups:
- Re: Invoking static class functions within call_user_functions
- From: Craig Taylor
- Re: Invoking static class functions within call_user_functions
- References:
- Invoking static class functions within call_user_functions
- From: Craig Taylor
- Invoking static class functions within call_user_functions
- Prev by Date: Invoking static class functions within call_user_functions
- Next by Date: Re: Invoking static class functions within call_user_functions
- Previous by thread: Invoking static class functions within call_user_functions
- Next by thread: Re: Invoking static class functions within call_user_functions
- Index(es):
Relevant Pages
|