php classe:从函数获取值到成员变量中时出现问题

发布于 2024-09-06 14:06:25 字数 694 浏览 8 评论 0原文

我有一个从数据库获取值并返回它的函数。我调用函数将其存储到成员变量中,但出现以下错误:

Parse error: parse error, expecting `','' or `';'' in I:\wamp\www\deposit\classes\Site.php on line 14

这是导致错误的行

public static $depositmoney = self::get_balance();

这是从数据库获取值的函数

    public static function get_balance()
    {
        global $link, $usertable, $userid, $useridentify;

        //query current balance
        $cb = mysqli_fetch_object(mysqli_query($link, "SELECT deposit FROM ".$usertable." WHERE ".$userid."=".$useridentify.""));
        return $cb->deposit;

    }//end of function get_balance().

所有这些代码都在同一个类中。有人知道导致错误的原因吗?

I have a function that gets a value from the database and returns it. I call the function to store it into a member variable but I get the following error:

Parse error: parse error, expecting `','' or `';'' in I:\wamp\www\deposit\classes\Site.php on line 14

This is the line that causes the error

public static $depositmoney = self::get_balance();

And this is the function that gets the value from the database

    public static function get_balance()
    {
        global $link, $usertable, $userid, $useridentify;

        //query current balance
        $cb = mysqli_fetch_object(mysqli_query($link, "SELECT deposit FROM ".$usertable." WHERE ".$userid."=".$useridentify.""));
        return $cb->deposit;

    }//end of function get_balance().

all of this code is in the same class. Anyone an idea of what's causing the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-09-13 14:06:25

类属性不能与运行时信息一起声明。

public static $depositmoney = self::get_balance();

上面的方法不会起作用。

请参阅 PHP 类属性手册:(重点是我的)

类成员变量称为“属性”。您还可能会看到它们使用其他术语(例如“属性”或“字段”)来引用,但出于本参考的目的,我们将使用“属性”。它们是通过使用关键字 public、protected 或 private 之一来定义的,后跟普通的变量声明。此声明可以包含初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时进行评估,并且不得依赖于运行时信息才能进行评估

您可以为 $depositmoney 创建一个 getter 并让它初始化当前未设置的值:

public static function getDepositMoney()
{
    if(self::$depositmoney === NULL) {
        self::$depositmoney = self::get_balance();
    }
    return self::$depositmoney;
}

但是,我建议摆脱 static 并使用实例方法和属性而是跟踪状态。您还需要摆脱 global 内容,并通过构造函数、setter 或在方法调用期间注入依赖项。这会减少耦合并使代码更易于维护。

Class properties may not be declared with run-time information.

public static $depositmoney = self::get_balance();

The above will not work.

See the PHP Manual on Class Properties: (emphasis mine)

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

You could create a getter for $depositmoney and have it initialize the value if it is currently unset:

public static function getDepositMoney()
{
    if(self::$depositmoney === NULL) {
        self::$depositmoney = self::get_balance();
    }
    return self::$depositmoney;
}

However, I suggest to get rid of the static and use instance methods and properties instead to track the state. You will also want to get rid of the global stuff and inject dependencies through the constructor, setters or during the method invocation. That decreases coupling and will make the code more maintainable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文