簡體   English   中英

在 PHP 7.4.2 中的類中的靜態函數中引用靜態變量時出錯

[英]Error referring static variable inside static function in a class in PHP 7.4.2

我正在學習 PHP 並使用 PHP 7.4.2(使用 XAMPP)版本來嘗試示例。 我正在嘗試使用 self::$vaiablename 從靜態函數訪問靜態變量

但收到以下錯誤:

Parse error: syntax error, unexpected 'self' (T_STRING) in C:\xampp\htdocs\php-getting-started\classes\CountryRepository.php on line 18


<?php

require 'Country.php';
require 'State.php';

class CountryRepository {
    private static $countries = array();

    protected static function init() {
        $countries = array();
        array_push($countries,
        new Country('Austria','at',array(new State('Styria'), new State('Burgandy'))));
        array_push($countries,
        new Country('United States','usa',array(new State('California'), new State('Maryland'))));
        array_push($countries,
        new Country('Luxembourg','lu'))

        self::$countries = $countries;
    }

    public static function getCountries() {
        if(count(self::$countries) === 0) {
            self::init();
        }
        return self::$countries;
    }
}

?>

任何人都可以幫我解決這個問題嗎?

在您的情況下,最后一個array_push之后缺少分號。 PHP 將此解釋為array_push()self ,從而導致上述錯誤。

new Country('Luxembourg', 'lu'))
                                ^^^

對比

new Country('Luxembourg', 'lu'));
                                ^^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM