Error using a function within a PHP array

Asked

Viewed 295 times

3

I’m using the laravel 5.2 and in my class IndexController I am trying to create an array with the function date('Y') thus:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class IndexController extends Controller
{   
    var $arr = array('ano' => date('Y'),
                     'titulo' => 'Titulo do projeto');

    public function Index()
    {        
        //passo o array para a view.       
        return view('welcome')->with($arr);
    }
}

but is returning me the following error:

syntax error, Unexpected '(', expecting ')'

Does anyone know why? I’m doing something wrong?

  • 4

    Take out the word var because it is PHP 4. Lavarel works only with PHP 5+

1 answer

6


The problem is that you are trying to create an array with a non-literal value It would probably be better to use a class member instead of trying to create a function-dependent value within the array.

How did you not explain exactly how you will use the $arr, I don’t know if this is good enough, but here’s a small example of how to initialize a given in the function constructor:

class MinhaClasse
{   
    public $arr = array();

    function __construct() {
        $this->arr['ano'] = date('Y');
    }

    public function getAno()
    {              
        return $this->arr['ano'];
    }
}

However, this generates a side effect, which can be seen in IDEONE:

http://ideone.com/gnqcuC

Since the value can change with each instantiation of the object, it is probably not what you want. It would be nice to specify at what point in fact you will need the value, because maybe it is the case of static, or pass the value in the constructor. If you will use static, needs a if( isset( ) ) in the constructor, if you don’t want to change the value of all instances and keep the first occurrence only (I think it leaves the code a bit "unsafe" inclusive, unless you have a deep domain of what you’re doing).

The var, in PHP 5 it is synonymous with public, but gives a Warning (or error, if you work with STRICT mode).

Maybe the solution is not to put the year in the creation of the class, but to define in PHP when using it even:

class MinhaClasse
{   
    public $arr = array();

    public function setAno( $ano ) {
        $this->arr['ano'] = $ano;
    }

    public function getAno()
    {              
        return $this->arr['ano'];
    }
}

And when it’s time to use:

$objeto = new MinhaClasse();
$objeto->setAno( date( 'Y' ) );
// agora vc usa o objeto onde precisar
  • But then you wouldn’t even have to declare $arr. If you were to call the class property inside the constructor, you should use $this-> or self:: (static). In the example, var $arr = array() serves no purpose whatsoever.

  • @Eduardoalmeida tidied up, it’s the rush to answer without testing, plus messing with a lot of language. Even more than PHP I pass away from OOP whenever I can, by overhead almost never compensate.

  • 1

    Just one more modification to make it perfect: remove the echo and puts a return. One should not put side effects (impression, ini_set()) in declaration code as a class or function.

  • 1

    @Eduardoalmeida actually put a demo of the "defect" that this causes in IDEONE, but I will accept your idea to be better pro forma. It remains to be seen if the OP really needs this information in each instance, or only in the first one (then it would be the case of some gambiarra in the constructor, like if( isset() )

  • Now, yes!!! + 1

  • @Eduardoalmeida actually I’m still not satisfied kkk, I need to know how the Meuchapeu will use this information, otherwise gets the feeling of "half-mouth" response dropped on the site :)

  • @Bacco, I want to use the information that is in the array to pass to the Laravel view, in case it will have other values beyond the year.. Only when I went to make a start Make that mistake..

  • @Would not Meuchapeu be the case to set the year in the call instead of inside the class? For example, before passing to the Standard, use a method $meuObjeto->setAno( 2016 )? I set an example in the answer

  • @Bacco, I don’t think it will be used inside the class, I added another value to the array and put as a step the array to the view...

Show 4 more comments

Browser other questions tagged

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