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
call or define? In the above example you define a function, which contradicts what you asked earlier.
– felipsmartins
not exactly the way you want it, but maybe anonymous functions serve you. What do you think?
– felipsmartins
Yes, maybe you want to reformulate and limit the scope of your question.
– felipsmartins
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.
– felipsmartins
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.
– felipsmartins