Error syntax error, unexpected T_OBJECT_OPERATOR

0

Good afternoon guys, how about a problem with this line of code, does anyone know why?

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/storage/2/3e/eb/noticiasdasgerais/public_html/yteste/Controller/index.php on line 7

Excerpt from line 7:

public function indexAction() {

    if (isset($_POST['busca']) && !empty($_POST['busca'])) {
        $busca = addslashes($_POST['busca']);
        $this->autos = (new ModelAuto)->get_all_busca($busca); //Linha 7
    } else {

        $this->autos = (new ModelAuto)->get_autos_destaque();
    }
    $dados = $this->get_autos();
    Tpl::View("site/index", $dados);
}

Thanks in advance!

    
asked by anonymous 22.05.2017 / 19:08

1 answer

1

In this line,

$this->autos = (new ModelAuto)->get_all_busca($busca);

Correct this way

$c = new ModelAuto;
$this->autos = $c->get_all_busca($busca);

In fact, to avoid redundancy, since both conditions need to instantiate the same object, it would look like this:

public function indexAction() {

    $c = new ModelAuto;
    if (isset($_POST['busca']) && !empty($_POST['busca'])) {
        $busca = addslashes($_POST['busca']);
        $this->autos = $c->get_all_busca($busca); //Linha 7
    } else {

        $this->autos = $c->get_autos_destaque();
    }
    $dados = $this->get_autos();
    Tpl::View("site/index", $dados);
}

The reason for the error is because it is using a version prior to PHP5.4 in which the feature that allows accessing members of a class during its instantiation has been added. link

  

Class member access on instantiation has been added, e.g. (new   Foo) - > bar ().

    
22.05.2017 / 19:36