2008-10-03 88 views
1

我目前春耕我的方式,通過IBM's tutorial on CakePHP我可以在類變量中添加沒有賦值的PHP數組鍵嗎?

有一次,我碰到這個代碼片段:

<?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/app/models/product.php中出現意外的','

我是PHP的n00b,所以我的問題是:是否允許創建一個數組沒有賦值的鍵?有沒有人玩過這個嘖嘖,知道什麼是?

回答

5

將值賦值爲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') 
); 
} 
?> 

這工作得很好。

3

它是合法的,但據我所知,你必須明確地說,這是通過分配空給它「空」,

$hasMany = array ('Product' => array(
'className' => 'Product', 
'conditions'=> null, // is this allowed? 
'order'=> null, // same thing here 
'foreignKey'=>'dealer_id')); 

你給的例子聽起來非常錯誤的,可能不應該工作,因爲它不是。

相關問題