php 这是什么意思?“解析错误:语法错误,意外的 T_PAAMAYIM_NEKUDOTAYIM”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1966010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:28:44  来源:igfitidea点击:

What does this mean? "Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM"

php

提问by openfrog

T_PAAMAYIM_NEKUDOTAYIM sounds really exotic, but most certainly absolutely nonsense to me. I traced it all down to this lines of code:

T_PAAMAYIM_NEKUDOTAYIM 听起来真的很奇特,但对我来说绝对是胡说八道。我将这一切追溯到这几行代码:

<?php
Class Context {
    protected $config;

    public function getConfig($key) { // Here's the problem somewhere...
    $cnf = $this->config;
    return $cnf::getConfig($key);
    }

    function __construct() {
    $this->config = new Config();
    }
}
?>

In the constructor I create a Config object. Here's the class:

在构造函数中,我创建了一个 Config 对象。这是课程:

final class Config {
    private static $instance = NULL;
    private static $config;

    public static function getConfig($key) {
    return self::$config[$key];
    }

    public static function getInstance() {
    if (!self::$instance) {
        self::$instance = new Config();
    }
    return self::$instance;
    }

    private function __construct() {
    // include configuration file
    include __ROOT_INCLUDE_PATH . '/sys/config/config.php'; // defines a $config array
    $this->config = $config;
    }
}

No idea why this doesnt work / what the error means...

不知道为什么这不起作用/错误意味着什么......

回答by benlumley

T_PAAMAYIM_NEKUDOTAYIM is the double colon scope resolution thingy PHP uses - ::

T_PAAMAYIM_NEKUDOTAYIM 是 PHP 使用的双冒号范围解析 - ::

Quick glance at your code, I think this line:

快速浏览一下您的代码,我认为这一行:

return $cnf::getConfig($key);

should be

应该

return $cnf->getConfig($key);

The first is the way to call a method statically - this code would be valid if $cnf contained a string that was also a valid class. The -> syntax is for calling a method on an instance of a class/object.

第一个是静态调用方法的方法 - 如果 $cnf 包含一个也是有效类的字符串,则此代码将是有效的。-> 语法用于在类/对象的实例上调用方法。

回答by tomasofen

Just my two cents for future visitors who have this problem.

对于有此问题的未来访问者,只需我的两美分。

This is the correct syntax for PHP 5.3, for example if you call static method from the class name:

这是 PHP 5.3 的正确语法,例如,如果您从类名调用静态方法:

MyClassName::getConfig($key);

If you previously assign the ClassName to the $cnf variable, you can call the static method from it (we are talking about PHP 5.3):

如果您之前将 ClassName 分配给 $cnf 变量,则可以从中调用静态方法(我们在谈论 PHP 5.3):

$cnf = MyClassName;
$cnf::getConfig($key);

However, this sintax doesn't work on PHP 5.2 or lower, and you need to use the following:

但是,此语法不适用于 PHP 5.2 或更低版本,您需要使用以下内容:

$cnf = MyClassName;
call_user_func(array($cnf, "getConfig", $key, ...otherposibleadditionalparameters... ));

Hope this helps people having this error in 5.2 version (don't know if this was openfrog's version).

希望这可以帮助人们在 5.2 版本中遇到此错误(不知道这是否是 openfrog 的版本)。

回答by Justin T. Watts

if you still need to use the double-colon then make sure your on PHP 5.3+

如果您仍然需要使用双冒号,请确保您使用的是 PHP 5.3+

回答by Question Mark

In your example

在你的例子中

return $cnf::getConfig($key)

Probably should be:

大概应该是:

return $cnf->getConfig($key)

And make getConfig not static

并使 getConfig 不是静态的

回答by richsage

The error is down to an "inappropriate use" of the double colon operator:

该错误归结为双冒号运算符的“不当使用”:

return $cnf::getConfig($key);

as by using the ::you're attempting to call a static method of the class itself. In your example you want to call a non-static method on an instantiated object.

就像通过使用::您试图调用类本身的静态方法一样。在您的示例中,您希望在实例化对象上调用非静态方法。

I think what you want is:

我想你想要的是:

return $cnf->getConfig($key);

回答by schnaader

It's the name for the ::operator

这是::运营商的名字

Wikipedia

维基百科

回答by Paul Tomblin

According to wikipedia, it means a "double colon" scope resolution operator.

根据维基百科,它的意思是“双冒号”范围解析运算符。

http://en.wikipedia.org/wiki/Scope_resolution_operator

http://en.wikipedia.org/wiki/Scope_resolution_operator