tags:

views:

65

answers:

4

Hi,

it seems I missed something important about arrays in PHP.

What's wrong with this:

var $condition = array('Status.name = ' => 'PUBLISHED');
var $paginate = array('conditions' => $condition );

this works just fine:

var $paginate = array('conditions' => array('Status.name = ' => 'PUBLISHED' ));
+1  A: 

Why the var keyword? Normally you wouldn't need this - unless these are fields on an object?. If so, you will need to reference them using $this. One of the following examples should work for you:

$condition = array('Status.name = ' => 'PUBLISHED');
$paginate = array('conditions' => $condition );

or

var $condition = array('Status.name = ' => 'PUBLISHED');
var $paginate = array('conditions' => $this->condition );

Without seeing more of the code, it is hard for me to say with certainty which one applies to you and/or if this will solve your problem. Hopefully it's pointed you in the right direction.

Splash
A: 

The var keyword is for declaring the class member variable and not for non-class variables.
The var keyword is supported in PHP5, albeit deprecated.

But for the var keyword, everything works as expected and we see the following when we dump the paginate array:

array(1) {
  ["conditions"]=>
  array(1) {
    ["Status.name = "]=>
    string(9) "PUBLISHED"
  }
}
codaddict
A: 

For me, both do not work. However, when i remove the var keyword from variables, both work perfectly well. Var keyword was used in php4.

Sarfraz
+3  A: 

The var part suggests me that you are defining a class. In that case, you cannot initialize an object variable with the content of another one; you can only initialize them with constants (which includes array).

<?php
  class test {
    var $test1 = array('test_11' => 10);
    var $test2 = array('test21' => $test1); // Error
  }
?>

If you need to initialize the content of a variable with the content of another one, then use the constructor.

<?php
  class test {
    function test() {
      $this->test1 = array('test_11' => 10);
      $this->test2 = array('test21' => $this->test1);
    }
  }
?>
kiamlaluno