【问题标题】:PHP7 define static variable for static class in Netbeans 8PHP7在Netbeans 8中为静态类定义静态变量
【发布时间】:2016-05-10 15:29:42
【问题描述】:

我使用 Netbeans 8 最新版本来编辑 PHP 7 代码(因为 Netbeans 8.1 仍然不支持 PHP 7),但 Netbeans IDE 说我做错了。什么是正确的方法:

<?php

class helloc {
    public static $first = 1;
}

class mainc {
    public static $another = NULL;

    public function example() {
        self::$another = new helloc();
        self::$another::$first = 2;
        echo self::$another::$first;
    }
}

$n = new mainc();
$n->example();

?>

使用 PHP7 的 NetBeans IDE 开发(内部版本 201605100002)表示此行出现错误:

self::$another::$first = 2;

意想不到的::

另外,这行也有错误:

回显自我::$another::$first; 意外::

这个的正确用法是什么? 如果我启动此代码,它将正常工作。没关系?还是 Netbeans IDE 的 bug?

如何声明 $another 变量? NULL 可以吗?还是其他方式? 在此示例中,我想将 $another 声明为静态“helloc”类。 我想从这个静态类访问变量。我知道,我可以声明一个 get/set 函数,它确实更好,但另一个问题是什么更好。

我只是想创建一个合适的 PHP7 代码。

【问题讨论】:

  • 我不明白...您正在尝试访问一个静态变量,该类在另一个静态变量中引用。这毫无意义!你不需要$self::$another::$first,因为$first 是静态的,helloc::$first 就足够了。在继续编码之前,您可能有一个概念问题需要解决;)。 PHP7 和 Netbeans 与此无关。
  • 但这也适用于我作为示例包含的内容。我是静态对象 + PHP 的新手 :-) 那么,静态类的定义是什么?至少有 1 个静态变量的类?或者一个类,什么定义为静态变量?像静态 $abc = NULL;然后 self:$abc = new xclass(); ?
  • 您使用的语法是有效的 PHP 7(但不是 PHP 5)语法。然而,这是非常不寻常的(因此 Netbeans 无法识别它也就不足为奇了),而且很可能不是您想要做的。通常不鼓励使用静态变量。

标签: php class netbeans static php-7


【解决方案1】:

更新 1,解决方案可能是针对我自己的问题:

class helloc {
    public static $first = 1;
    public $last = 3;
}

class mainc {
    public $another = NULL;
    public function example() {
        helloc::$first = 2;
        echo helloc::$first;
        $this->another = new helloc();
        $this->another->last = 4;
        echo $this->another->last;
    }
}

$n = new mainc();
$n->example();

那么,如果类中有静态和非静态变量,这就是 PHP7 的正确用法吗?

访问静态变量,不需要使用new()。 对于非静态需要用new()来创建吧?

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多