How to transform a string into an object? [closed]

2

I have the following String :

$exp = "(10-9)+(7-6)";

Let's assume that I process this String and is able to return the following result in the String format:

$exp = "new Expressao(
    new Expressao(new Numero(10),new Operador('-'),new Numero(9)),
    new Operador('+'),
    new Expressao(new Numero(7), new Operador('-'),new Numero(6))
)";

How do I dynamically create a Expressao object as indicated in String ?

    
asked by anonymous 05.12.2016 / 22:20

4 answers

3

Just as Tiago Luz replied, eval would do well for you, since you are treating the string to only receive allowed characters and I believe you create the string yourself with the objects, right?

An example with eval would look something like this.

<?php
class Expressao {
    private $val;
    private $nu1;
    private $nu2;
    private $ope;
    function __construct($nu1, $ope, $nu2) {
        $this->nu1 = $nu1;
        $this->ope = $ope;
        $this->nu2 = $nu2;
    }
};
class Numero {
    private $val;
    function __construct($val) {$this->val = $val;}
};
class Operador {
    private $val;
    function __construct($val) {$this->val = $val;}
};
$exp = "new Expressao(
    new Expressao(new Numero(10),new Operador('-'),new Numero(9)),
    new Operador('+'),
    new Expressao(new Numero(7), new Operador('-'),new Numero(6))
)";

$r = eval('return ('.$exp.');');

var_dump($r);

Note that I concatenate a return with the expression, this is so that the variable $r receives the result of the expression and not only that it is executed. It is as if eval throws your code inside a function and executes, so to receive something from this function you need to give return .

I hope it has become clear.

I still recommend that you do all this parser in class Expressao , something like

$exp = new Expressao('(10-9)+(7-6)');

And in there you create a function that treats the string "(10-9)+(7-6)" and turns it into your objects.

You can also create "dynamic" objects, for example:

function getObj($val) {
    if (in_array($val, explode('', '(+-)'))) {
        $class = 'Operador'; 
    } else if (preg_match('/^[0-9]+$/', $val) > 0) {
        $class = 'Numero';
    } else return null;

    return new $class($val);
}

But I'm not having the time to work out this code ↑

    
07.12.2016 / 11:56
0

You can do this with the eval($code) function of php.

Whatever is in $code will run as if it were PHP code. Beware of possible security holes!

This link is the explanation.

    
06.12.2016 / 00:46
0

As a matter of curiosity

Searching I was able to find a way to do with what eval() create the object as I wanted it using a closure

public function criaObjeto($obj){
        eval("\$newObj = function(){return $obj;};");
        return $newObj();
}

I do not have full control over working with eval() to state if it does not cause a security flaw.

In my code I put a function that sanitizes data entry allowing only caracteres numéricos , ( , ) , + and - .

class Validacao{
    private function limpaString($obj){
        return preg_replace("/[^0-9\+\-\(\)]/", "", $obj);
    }
}

However, I'm not entirely sure about security.

    
07.12.2016 / 14:41
-2

What would be appropriate for you to do this:

$ExpressaoObj->new Expressao();

Only your code is confusing and with errors!

    
05.12.2016 / 22:35