PHP: using variables in associative arrays?
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('cond开发者_开发百科itions' => array('Status.name = ' => 'PUBLISHED' ));
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.
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);
}
}
?>
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"
}
}
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.
精彩评论