Pratig
Pratig

Reputation: 148

how to call a function in php using string

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.

Upvotes: 0

Views: 47

Answers (1)

Richard
Richard

Reputation: 2815

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);

Upvotes: 1

Related Questions