i have a function inside a Class that is inside another, this way i can organize all my ‘module’s engines’ in different folders and call they functions.
For example, i have a module called ‘mdnewsletter‘, that module has other classes inside like: ‘setting’, ‘newsletter’, etc.
Normally, if i wan’t to get the newsletter vars with id ‘5’ i do this:
$res = $mdnewsletter->newsletter->get(5);
if($res===false) { /* error handler here */ }
/* otherwise, the code here */
But now i have a module called ‘mdapi’. The propose of this module is to securely load another module’s engine and execute commands only for modules.
My question is on this code snippet:
$response = @eval($eval);
if($response===false && ( $error = error_get_last() )){
$this->error[] = "mdapi->exec() | A função fornecida não é uma função válida";
$can_exec = false;
$eval = '';
$response = false;
};
As i get the execution string has ‘mdnewsletter->newsletter->get’ and arguments in other var, i can’t use method_exists(). I really prefer to read the parse error code.
If an parse error occurs, the php code will hang and i can’t inform the other side that the code is bad or an error occurred.
How can i fix this?
EDIT:
Based on Oktopus answer, i’ve wrote a code that dynamically checks all the objects behind the function and then it test the function with last object, something like this:
// Avaliar se a primeira parte é uma class e os restantes funcoes
$tmp = explode ('->', $tmp);
// Verificar se são objectos até à função (ultima string)
$obj = $$tmp[0];
for($i=0;$i<count($tmp)-1;$i++){
if($i!=0){
$obj = $obj->$tmp[$i];
}
if(!is_object($obj)){
$this->error[] = "mdapi->exec() | A variavel '".$tmp[$i]."' não é um objecto!";
$can_exec = false;
$eval = '';
$response = false;
}
}
// Verificar se a ultima variavel é uma função no ultimo objecto
if(!is_callable(array($obj, $tmp[count($tmp) - 1]))){
$this->error[] = "mdapi->exec() | A função pedida não foi encontrada no ultimo objecto!";
$can_exec = false;
$eval = '';
$response = false;
}
I won’t go in the “it may be insecure thing”, but here is how you could do it :