Jump to content

Call function from class by string.


Chris92

Recommended Posts

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!

Link to comment
Share on other sites

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 :P:

Fatal error: Using $this when not in object context

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.