我目前正在研究 IBM's tutorial on CakePHP
有一次我遇到了这段代码:
<?php
class Dealer extends AppModel {
var $name = 'Dealer';
var $hasMany = array (
'Product' => array(
'className' => 'Product',
'conditions'=>, // is this allowed?
'order'=>, // same thing here
'foreignKey'=>'dealer_id'
)
);
}
?>
当我运行它时,我收到以下错误消息:“解析错误:语法错误,意外的‘,’在/Applications/MAMP/htdocs/cakephp/app/models/product.php 的第 7 行”
我是 PHP 的新手,所以我的问题是:是否允许使用没有分配值的键创建数组?有没有人玩过这个图并知道发生了什么事?
最佳答案
分配值 null 而不是遗漏任何内容。 manual says
isset() will return FALSE if testing a variable that has been set to NULL
<?php
class Dealer extends AppModel
{
var $name = 'Dealer';
var $hasMany = array(
'Product' => array(
'className' => 'Product',
'conditions' => null,
'order' => null,
'foreignKey' => 'dealer_id'
)
);
}
?>
这很好用。
关于php - 我可以在类变量中添加没有赋值的 PHP 数组键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/166615/