クラス変数に値が割り当てられていないPHP配列キーを追加できますか?
質問
現在、を使用しています。 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'
)
);
}
?>
実行すると、次のエラーメッセージが表示されます:&quot;解析エラー:構文エラー、予期しない '、' /Applications/MAMP/htdocs/cakephp/app/models/product.phpの7行目&quot;
私はPHPのn00bなので、質問は次のとおりです。値が割り当てられていないキーを持つ配列を作成できますか?誰かがこのツタンで遊んで、何が起きているのか知っていますか?
解決
何も残さずにnull値を割り当てます。 マニュアルによると
NULLに設定された変数をテストする場合、isset()はFALSEを返します
<?php
class Dealer extends AppModel
{
var $name = 'Dealer';
var $hasMany = array ('Product' => array(
'className' => 'Product',
'conditions'=> null,
'order'=> null,
'foreignKey'=>'dealer_id')
);
}
?>
これは正常に動作します。
他のヒント
それは合法ですが、私が知る限り、nullを割り当てることで明示的に「空」と言わなければなりません
$hasMany = array ('Product' => array(
'className' => 'Product',
'conditions'=> null, // is this allowed?
'order'=> null, // same thing here
'foreignKey'=>'dealer_id'));
指定した例は非常に間違っているように聞こえますが、おそらく動作しないはずです。
所属していません StackOverflow