Perad Posted October 4, 2009 Share Posted October 4, 2009 I am trying to create a dynamic function caller. It should execute like this. $url is an array. $url[1] contains the function. $class->$url[1](); Any idea how I can make this work? Is there some sort of php function to do this? Quote Link to comment https://forums.phpfreaks.com/topic/176453-how-do-i-call-a-class-function/ Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 How do you store functions in this array? As a string? As a closure? Quote Link to comment https://forums.phpfreaks.com/topic/176453-how-do-i-call-a-class-function/#findComment-930119 Share on other sites More sharing options...
Perad Posted October 4, 2009 Author Share Posted October 4, 2009 As a string. $url[1] = 'login'; Quote Link to comment https://forums.phpfreaks.com/topic/176453-how-do-i-call-a-class-function/#findComment-930122 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 So you're in fact storing function identifiers, not functions themselves <?php class foo { public function bar() { echo 'bar'; } } $url = array( 0 => 'bar' ); $foo = new foo(); $foo->$url[0](); this works for me Quote Link to comment https://forums.phpfreaks.com/topic/176453-how-do-i-call-a-class-function/#findComment-930126 Share on other sites More sharing options...
fkaufusi Posted October 11, 2009 Share Posted October 11, 2009 call_user_func($foo->url[0]); Quote Link to comment https://forums.phpfreaks.com/topic/176453-how-do-i-call-a-class-function/#findComment-934690 Share on other sites More sharing options...
zmoerf Posted October 11, 2009 Share Posted October 11, 2009 this sample class code : <?php class Page { var $Title; var $Keywords; var $Content; function Display( ) { echo "<HTML>\n<HEAD>\n"; $this->DisplayTitle( ); $this->DisplayKeywords( ); echo "\n</HEAD>\n<BODY>\n"; echo $this->Content; echo "\n</BODY>\n</HTML>\n"; } function DisplayTitle( ) { echo "<TITLE>" . $this->Title . "</TITLE>\n"; } function DisplayKeywords( ) { echo '<META NAME="keywords" CONTENT="' . $this->Keywords . '">'; } function SetContent( $Data ) { $this->Content = $Data; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176453-how-do-i-call-a-class-function/#findComment-934854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.