Variable method name

Asked

Viewed 50 times

-2

I’ve had this doubt before but I let it go, now I ask:

Example, I have 1 function:

public function nome_variavel(){
    // A função em si nunca muda, preciso apenas que o nome que ela é definida seja variável.
}

I would like to define this function by a variable name, example:

$nome_da_funcao = "nome_generico";
public function $nome_da_funcao(){} // Apenas exemplo para entendimento

Is that possible? If so, in what way?

  • call or define? In the above example you define a function, which contradicts what you asked earlier.

  • not exactly the way you want it, but maybe anonymous functions serve you. What do you think?

  • Yes, maybe you want to reformulate and limit the scope of your question.

  • It would be helpful if you told why you need it and in what cases it is necessary. That way it is easier to delineate.

  • 1

    I haven’t seen your sources, but surely you’re doing this inappropriately, it’s not possible that you need to create a new function for each new customer. In any case, you might want to take a look at the magical method called __call(), and from it, invoke/delegate the/to function whose block you say never change. Let us know if this solves your case.

1 answer

2


I believe this is not possible and even if it was I think it would not be a very cool practice.

If I understand the your comment, I recommend you maybe create a array associative, I will try to formulate an example, I know it is not the format you are using, but should help.

Requires 5.3.0 or higher

php app.

class App
{
     private $views = array();

     public function page($nome, $function)
     {
           if (is_callable($function)) {
               $this->view[$nome] = $function;
           }
     }

     public function exec()
     {
           $page = $_GET['page'];

           if (empty($this->views[$page]) === false) {
                $caller = $this->views[$page];
                $caller();//Executa método
           }
     }
}

index php.

<?php
require_once 'app.php';

$app = new App;
$app->page('nome_generico', function() {
    echo 'Olá mundo';
});
$app->page('sobre', function() {
    echo 'Eu sou Thyago';
});
$app->page('foo', function() {
    echo 'Algo aqui';
});
$app->exec();
  • When to access http://localhost/projeto/?page=nome_generico is displayed this:

    Hello world

  • When to access http://localhost/projeto/?page=sobre is displayed this:

    I am Thyago

  • When to access http://localhost/projeto/?page=foo is displayed this:

    Something here

  • 1

    Ow William, that’s exactly it! Since I never used anything of the kind, I came here precisely to know if there would be something native, anyway I thought not to mess with the framework’s action structure, but really it would be impossible, but this path was all I needed as an alternative, very well explained and detailed your answer.

  • @Thyagothysoft I don’t know if it makes a difference to you, but there were two errors in the script, I fixed it.

  • Of course it does, for me and for anyone who wants to do something like I rs. Actually I haven’t tested yet, I will do the test tomorrow, but I could see that the condition did not check anything in Epsy. Thank you Guilherme!

Browser other questions tagged

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