【问题标题】:Cannot Create Spldoublylinkedlist无法创建 Spldoublylinkedlist
【发布时间】:2014-01-21 06:33:35
【问题描述】:

我是一名新手 PHP 开发人员,最近从 Java 开发人员那里迁移过来。对于我最初的教程,我已经开始执行网络上提供的示例、演示 PHP 程序。

我目前正在练习 PHP 框架提供给我们的 PHP 集合,特别是 SplDoubleLinkedList。

我有一个名为 Awards 的类,我试图在其中创建一个字段,类型为 SplDoubleLinkedList 的 _dll。

我的 Awards 类的源代码如下:

Awards.php

    <?php

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

    /**
     * This is the Award class
     *
     * @author Razor
     */
    class Award {
        $_dll = new SplDoublyLinkedList();

        function executeCode() {
            $_dll->push(2);
            $_dll->push(3);
            $_dll->unshift(5);

            var_dump($_dll);
        }
    }

这里的问题是,尽管从here 复制粘贴代码,编译器在我声明$_dll 字段的那一行返回一个语法错误。错误是,unexpected: variable $_dll. expected: function, const, use, var, }, VAR_COMMENT, static, abstract, final, private, protected, public

谁能告诉我我到底哪里出错了?尽早回复将不胜感激。提前谢谢你。

【问题讨论】:

    标签: java php collections linked-list


    【解决方案1】:

    在声明属性时,您需要指定它是publicprivate 还是protected(或其他一些选项,但这些是最有可能的)。而且您不能在声明时分配new SplDoublyLinkedList(),因此您需要在构造函数中处理该初始化。

    class Award {
        protected $_dll;
    
        public function __construct()
        {
            $_dll = new SplDoublyLinkedList();
        }
    }
    

    【讨论】:

    • 非常感谢您的回复。那是我太愚蠢了,没有注意到这一点。哈哈=D
    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 2014-06-30
    • 2021-01-11
    • 2022-12-11
    • 2015-09-16
    • 2021-04-18
    • 2014-11-21
    • 2018-05-12
    相关资源
    最近更新 更多