Scenario
I use a method to instantiate classes and methods dynamically.
Properties received:
-
modulo
= name of folder with files.class.php
-
ferramenta
= name of file.class.php
-
acao
= name of method
Method that executes the request:
private function executar()
{
try {
# Monta o patch e faz require da classe
$classe = DIR_MODULOS . $this -> modulo . DS . $this -> ferramenta . '.class.php';
require_once $classe;
# Instancia o objeto e executa o método
$obj = new $this -> ferramenta();
$resposta = $obj -> {$this -> acao}($this -> dados);
# Retorna a resposta
$this -> retorno = $resposta;
} catch (Exception $e) {
$this -> error = $e->getMessage();
}
}
Problems
- If the
modulo
property is incorrect, it will not find the folder path.- Error: Warning and Fatal Error require .
- If the
ferramenta
property is incorrect, it will not find the class file.- Error: Warning and Fatal Error require .
- If the% cos_de% property is incorrect, you will not find the method in the class.
- Error: Call method error: Fatal error: Call to undefined method
Doubt
-
What is the best way to treat errors since
acao
does not treat them?(preferably native functions)
Objective
The idea is to return only a simple string as the error.
Example:
- "Invalid module"
- "Invalid tool"
- "Invalid action"