Calling static functions with a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Calling static functions with a variable

Post by Todd_Z »

Code: Select all

$class = "Class_Name";
$function = "Function_Name";
$variable = true;
$class::$function( $variable );
Why does the preceeding code give me an error? Is there another way to call a static method using a variable?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can call the method using a variable but accessing the class statically requires the need to use it's actual identifier by the look of it.

I've been trying this with PHP5... not sure what you're using.

EDIT | And if you need to make the class name variable I think you'll just have to work-around it by creating an instance of it first. You can create instances of classes using variable class names.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I'm using php5 too.

I'm really just making up for the lack of namespaces. Its really just a singleton class, i want to group the functions together.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ahem

Code: Select all

call_user_func(array($class, $function), $variable);
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

touche feyd, touche
Post Reply