问题描述
我很难理解为什么在以下代码中出现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,
不幸的是,没有提供关于以前版本中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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!