Syntax error, Unexpected T_OBJECT_OPERATOR

Asked

Viewed 2,157 times

1

Good afternoon guys, I have a problem with this line of code, someone knows 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

Section of 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!

  • Which version of php you’re using?

  • The version is 5.2.

  • This syntax only works from php5.4 forward.

1 answer

2


Along those lines,

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

Fix it that way

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

Moreover, to avoid redundancy, since both conditions need to instantiate the same object, would be 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 you are using a previous version of PHP5.4 in which you added the feature that allows you to access members of a class during your instantiation. http://docs.php.net/manual/en/migration54.new-features.php

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

  • How would I do then in a function that accesses multiple classes? For example: public Function get_autos() {
 $dados = array(
 'auto' => $this->autos['dados'],
 'paginacao' => $this->autos['paginacao'],
 'auto_recente' => (new ModelAuto)->get_autos_recente(),
 'auto_popular' => (new ModelAuto)->get_autos_popular(),
 'auto_topo' => (new Modelauto)->get_autos_topo(), 'slide' => (new Modelslide)->getAll(1), ...

  • 'parceiro' => (new ModelSlide)->getAll(5),
 'servico' => (new ModelServico)->getAll(),
 'depoimento' => (new ModelDepoimento)->getAll(),
 'social' => (new ModelSocial)->getById(),
 'marca' => (new ModelMarca)->getAll(), 'model' => (new Modelmodel)->getAll(), 'agencia' => (new Modelgencia)->getById(), 'config' => (new Modelconfig)->getById()

  • read the answer...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.