Chris92 Posted November 18, 2010 Share Posted November 18, 2010 Hi I'm setting up my own framework for a project. I was coming to the point of completion and thought up of a new idea. Is it possible to call a function like this from a class: class myClass { function func($param) { echo "Hello world". $param; } } $class = 'myClass'; $func = "func(' It\'s time to partey!')"; $$class = new $class; $$class->$func; Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/219073-call-function-from-class-by-string/ Share on other sites More sharing options...
trq Posted November 18, 2010 Share Posted November 18, 2010 call_user_func. Quote Link to comment https://forums.phpfreaks.com/topic/219073-call-function-from-class-by-string/#findComment-1136055 Share on other sites More sharing options...
Chris92 Posted November 18, 2010 Author Share Posted November 18, 2010 I'm not sure if that's the one I'm looking for. If I'm using it correctly: class myClass { function func($param) { echo "Hello world". $param; } } $class = 'myClass'; $func = "func"; $str = ' It\'s time to partey!'; $$class = new $class; call_user_func($$class->$func, $str ); throws this error: Notice: Undefined property: myClass::$func Making me guess that it's trying to call it as a variable rather than function. Ignore that! I've gotten to this error now : Fatal error: Using $this when not in object context Quote Link to comment https://forums.phpfreaks.com/topic/219073-call-function-from-class-by-string/#findComment-1136056 Share on other sites More sharing options...
ManiacDan Posted November 18, 2010 Share Posted November 18, 2010 No you haven't, there's no $this in that code snippet. The error you're getting is related to you not having the proper callback format as the first argument to call_user_func. That being said...what's with the variable variables? Why are you doing it like this? It's confusing and sloppy and will make maintaining this code hell. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/219073-call-function-from-class-by-string/#findComment-1136064 Share on other sites More sharing options...
Chris92 Posted November 18, 2010 Author Share Posted November 18, 2010 Yeah, I just wrote that code for a quick example. What I'm doing is writing a framework which allows dynamic templates to call functions from different plug-ins example: <div id="twitter"> {PLUGIN_twitter_tweets(5)} </div> This is then parsed by my template system, and then gets the plug-in required (in this case twitter), and call the function "tweets" and pass 5 as a parameter. Here's something like what I've made class template { // shit load of functions, constants blah blah // this function gets called after a few tests // it replaces all the {PLUGIN_*} tags it can. function plugin() { preg_match_all( '~{PLUGIN_[^}]+}~', $this->html, $array ); foreach( $array[0] as $value ) { $value = str_replace( array('{', 'PLUGIN_', '}'), '', $value ); $plugin = explode( '_', $value, 2 ); $this->load->plugin($plugin[0]); // loads the PHP class (once) $$plugin[0] = new $plugin[0]; $str = call_user_func( array( $plugin[0], $plugin[1] ) ); // this is the bit where I'm stuck $this->html = str_replace( '{PLUGIN_' . $value . '}', $str, $this->html ); } } } Twitter class class twitter { // another load of functions and constants are here... function tweets($limit, $image=true, $links=true, $name=true, $sname=true) { $object = $this->getTweets($limit); $html = ""; foreach( $object as $tweet ) { $html .= "<div class=\"tweet\">\n". ( ( $image === true ) ? "<a href=\"http://twitter.com/{$this->username}\" target=\"_blank\"> <img src=\"{$tweet->user->profile_image_url}\" alt=\"{$tweet->user->name}\" /> </a>\n" : '' ). ( ( $sname === true ) ? "<span class=\"screen-name\"><a href=\"http://twitter.com/{$this->username}\" target=\"_blank\">{$tweet->user->screen_name}</a></span>\n" : '' ). ( ( $name === true ) ? "<span class=\"name\"><a href=\"http://twitter.com/{$this->username}\" target=\"_blank\">{$tweet->user->name}</a></span>\n" : '' ). "<p class=\"text\">" . ( ( $links === true ) ? $this->links($tweet->text) : $this->text ) . "</p>" . "<p class=\"time\">" . $this->time($tweet->created_at) . ".</p>" . "</div>\n"; } return $html; } } So as you can see it will requite that I can refer to children and ancestors. Quote Link to comment https://forums.phpfreaks.com/topic/219073-call-function-from-class-by-string/#findComment-1136089 Share on other sites More sharing options...
Chris92 Posted November 19, 2010 Author Share Posted November 19, 2010 I made a nice solution, here's an example: <?php class twitter { var $v = "Hi"; function tweets($i, $b) { return $this->v . $i ."? ". $b; } } $str = "twitter_tweets(5, true)"; $plugin = explode( "_", $str, 2 ); // array( [0] => twitter, [1] = tweets(5, true) ) $func = explode( "(", $plugin[1], 2 ); // array( [0] => tweets, [1] => 5, true) ) $args = substr( $func[1], 0, (strlen($func[1])-1) ); // 5, true $args = explode( ",", $args ); // array( [0] => 5, [1] => true ) // trim whitespace in arguments foreach( $args as $key => $val ) { $args[$key] = trim($val); } $$plugin[0] = new $plugin[0]; // $twitter = new twitter echo call_user_func_array( array( $$plugin[0] , $func[0] ), $args ); // $twitter->tweets(5,true) ?> Quote Link to comment https://forums.phpfreaks.com/topic/219073-call-function-from-class-by-string/#findComment-1136568 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.