1

I want to execute a string containing static function call

$string="ClassName::function()";

Let ClassName=ABC & function=abc
so

$string="ABC::abc()";

the function abc returns a array

now what i want is

$array=$string;

It should execute $string and store the returned array into $array

3
  • @Uchiha I think OP want eval. But everybody abuses using of it Commented Jul 6, 2015 at 7:30
  • it doesn't work when implemented shows Parse error: syntax error, unexpected end of file Commented Jul 6, 2015 at 7:32
  • @splash58 Yep you're right Commented Jul 6, 2015 at 7:33

3 Answers 3

3

You can use eval, but use this with extreme caution.

$string = 'Class::function();';
$array = eval($string);
Sign up to request clarification or add additional context in comments.

4 Comments

it doesn't work when implemented shows Parse error: syntax error, unexpected end of file
Do you have a semicolon after $string?
the semicolon worked dude. Thanks a lot for HELP. what a silly mistake i made.
If you think one of the answers was the final solution, please accept that answer.
0

You can use such exotic construction to avoid using eval

class ABC {

  static function abc_func() {
    echo 'abc';
  }
}
$string="ABC::abc_func()";

$i = preg_split('/::|\(\)/', $string);

$i[0]::{$i[1]}();  // abc echoed

Comments

0

Try this, call a static method directly by setting the class name is a variable :

$classname = 'ABC';
$array = $classname::abc(); 

1 Comment

it doesn't work when implemented shows Parse error: syntax error, unexpected end of file

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.