我可以在类变量中添加没有分配值的PHP数组键吗?
时间:2020-03-06 15:04:20 来源:igfitidea点击:
我目前正在研究关于CakePHP的IBM教程
在某一时刻,我遇到了这段代码:
<?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' ) ); } ?>
运行它时,出现以下错误消息:"解析错误:语法错误,意外的','在第7行的/Applications/MAMP/htdocs/cakephp/appphp/apps/models/product.php中"
我在PHP上是n00b,所以我的问题是:是否可以使用没有指定值的键组成数组?有人玩过这个tut,知道怎么了吗?
解决方案
这是合法的,尽管据我所知,我们必须通过为其分配null来明确表示它为"空",
$hasMany = array ('Product' => array( 'className' => 'Product', 'conditions'=> null, // is this allowed? 'order'=> null, // same thing here 'foreignKey'=>'dealer_id'));
我们提供的示例听起来很错误,可能不应该这样做,因为事实并非如此。
将值分配为null,而不遗漏任何内容。手册说
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') ); } ?>
这很好。