0
class AC{
    public $name;

    function __construct($name)
    {
        $this->name = $name;
    }

    function getName()
    {
        return $this->name; 
    }
}

$string = 'abc=new AC("Example");';

$string contains a string which is used to call class AC. I want to execute this function and call getName() function of AC class.

4
  • 2
    You really need to rethink this, but if you INSIST on doing it, php.net/eval, and may Alan Turing have mercy on your soul. Commented Jun 24, 2015 at 19:15
  • Shall I create new class pass this variables to them and execute them; @MarcB Commented Jun 24, 2015 at 19:22
  • What is the desired goal after this aproach? Commented Jun 24, 2015 at 19:27
  • @DarkBee Above string is showing only function calling once but in reality it calls function more than 100 times. dont worry ,i have figured it out. Commented Jun 24, 2015 at 19:55

1 Answer 1

1

You can use this:

$className = "AC";
$obj = new $className($params); // Pass params as array for example, if you want maximal dynamic (or make the number of arguments dynamic)

And for functions:

$func = "getName";
$obj->$func($params);
Sign up to request clarification or add additional context in comments.

2 Comments

$obj should also a string
@user1675595: Then you make $$str->$func($params);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.