PHP - online lambda functions

2

Dear, I have a system where the user (with development permission) can schedule some online routines, to be executed within the system, without the need to create file and call via require / include.

To execute this function, I use create_function, but the problem is that when creating this function within a loop for example, the error "can not redeclare class xxxx" occurs.

If the routine is created like this: class aaa{/codigo/}

And I looped to execute:

for ($i = 1; $i <= 5; $i++) {
  $funcao = create_function('', $codigoLidoDoBD);
  $funcao();
}

The error occurs because even though the function name is different, the aaa class has already been declared and registered somewhere: S

Obs : I could use class_exists in each class, but there are too many to change ...

Obs2 : Sometimes even declaring create_function out of loop and calling only $funcao() , the error also occurs.

Does anyone know how to solve it?

    
asked by anonymous 18.03.2015 / 19:52

2 answers

2

Unless you create a parser for before actually executing the validated code you will have problems.

Dynamic inclusion of code without rules or interfaces provides a number of problems and risks.

What you could do is test code compatibility before running it with block try..catch

Another possibility is to add dynamic namespaces like this, but even if it works is a bad practice.

Another point is to always try to compile these inclusions into files and use that file instead of pulling it from the database. Of course, by changing the database the compilation is executed again and if there is an error you can alert the user and not perform the compilation,

I hope it helps.

    
19.03.2015 / 00:13
1

My proposal would be to rename the class to avoid loading and collision between two or more classes with the same name. It would make a <textarea> where the user can register all the functions of the class that wishes.

// <textarea>

// functions
public function myname()
{
    echo 'Papa Charlie';
}

public function myage()
{
    echo 'Quase 33 :)';
}

At the time the user submits the form, you create the class name by reference to the user name - or its ID - and ensures that there will be no collision with the names.

$content = 'class UserFulanoDeTal_ID_User
{
    ' . $_GET['myfunctions'] . '
}';

When you write $content to your DB, it will already be renamed. When loading, just use USER NAME and USER ID of the user to invoke the class and its functions.

    
19.03.2015 / 21:15