Why am I not able to create a function within a class named after the array list of a class method in php?

Asked

Viewed 40 times

-6

Why am I unable to create a function within a class named after the array list of a class method in php?

function $this->info[0](){

}
  • The function name is $this->info[0]() ? has details missing in your question.

  • Dude, that’s just scaffolding. PHP is dynamic, but not so much.

  • 1

    So you don’t get confused with the negatives, understand that the fact that the question is not clear and outside the scope of the site.

1 answer

0


There is no way to do this in PHP. The most that can be done is a dynamic method call, but not a statement:

 class Aluno {

      public function __construct($nome)
      {
         $this->nome = $nome;
      }
      public function nome()
      {
           return $this->nome;
      }
 }

The call could be made like this:

$aluno = new Aluno('Wallace');
$aluno->{$this->info[0]}();

What you are trying to do, in your example, causes a syntax error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.