【问题标题】:Can I add a PHP array key without an assigned value in a class variable?我可以在类变量中添加一个没有赋值的 PHP 数组键吗?
【发布时间】:2010-09-15 01:29:04
【问题描述】:

我目前正在努力通过IBM's tutorial on CakePHP

有一次我遇到了这个 sn-p 代码:

<?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,所以我的问题是:是否允许使用没有赋值的键创建数组?有没有人玩过这个 tut 并且知道发生了什么?

【问题讨论】:

    标签: php arrays cakephp


    【解决方案1】:

    分配值 null 而不是遗漏任何内容。 manual says

    如果测试已设置为 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')
    );
    }
    ?>
    

    这很好用。

    【讨论】:

      【解决方案2】:

      这是合法的,但据我所知,您必须通过将 null 分配给它来明确表示它是“空的”,

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

      您给出的示例听起来非常错误,并且可能不应该工作,因为它不是。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-25
        • 2022-01-22
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多