PHP 5.2.x中出现意外的T_PAAMAYIM_NEKUDOTAYIM [英] Unexpected T_PAAMAYIM_NEKUDOTAYIM in PHP 5.2.x

查看:53
本文介绍了PHP 5.2.x中出现意外的T_PAAMAYIM_NEKUDOTAYIM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解为什么在以下代码中出现Unexpected T_PAAMAYIM_NEKUDOTAYIM错误,这对我来说似乎是完全有效的...

I'm having a hard time understanding why I'm getting an Unexpected T_PAAMAYIM_NEKUDOTAYIM error in the following code, which seems perfecly valid to me...

class xpto
{
    public static $id = null;

    public function __construct()
    {
    }

    public static function getMyID()
    {
        return self::$id;
    }
}

function instance($xpto = null)
{
    static $result = null;

    if (is_null($result) === true)
    {
        $result = new xpto();
    }

    if (is_object($result) === true)
    {
        $result::$id = strval($xpto);
    }

    return $result;
}

PHP 5.3+中的输出:

echo var_dump(instance()->getMyID()) . "\n"; // null
echo var_dump(instance('dev')->getMyID()) . "\n"; // dev
echo var_dump(instance('prod')->getMyID()) . "\n"; // prod
echo var_dump(instance()->getMyID()) . "\n"; // null

以前的版本中,我不能执行$result::$id = strval($xpto);,有人知道为什么吗?

In prior versions however, I can't do $result::$id = strval($xpto);, does anyone know why?

有没有解决此问题的方法?

Are there any workarounds for this problem?

推荐答案

看完键盘后:

if (is_object($result) === true)
{
    $result::id = strval($xpto);
}

...应该是

if (is_object($result) === true)
{
    $result::$id = strval($xpto);
}

我在一个新粘贴中对此进行了更正,但错误仍然存​​在...只是在演示代码中告知您问题所在.

I corrected this in a new paste, and the error still exists... just letting you know about the problem in the demo code.

编辑

每个PHP文档页面上的static关键字

Per PHP documentation page on static keyword,

从PHP 5.3.0开始,可以 使用变量引用类. 变量的值不能为 关键字(例如,自我,父母和 静态).

As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

不幸的是,没有提供关于以前版本中WHY的详细信息,也没有看到注释中提供的解决方法.

Unfortunately, no detail is given as to WHY to was otherwise in prior versions, nor do I see a workaround presented in the comments.

但是,由于该类是静态的,因此您应该能够直接更改该属性:

Because the class is static, though, you should be able to change the property directly:

function instance($xpto = null)
{
    static $result = null;

    if (is_null($result) === true)
    {
        $result = new xpto();
    }

    if (is_object($result) === true)
    {
        xpto::$id = strval($xpto)
    }

    return $result;
}

这篇关于PHP 5.2.x中出现意外的T_PAAMAYIM_NEKUDOTAYIM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆