Unexpected T_PAAMAYIM_NEKUDOTAYIM in PHP 5.2.x
Asked Answered
M

5

6

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;
}

Output in 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

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

Are there any workarounds for this problem?

Milore answered 14/2, 2011 at 17:57 Comment(4)
I copy/pasted your code to a php file, and it runs without errors.Tautog
@Chris: What PHP version are you using? Also, check this link: codepad.org/wpT0g3VH.Milore
Sorry, misinterpreted your question. Posted answer below.Tautog
Possible duplicate of PHP expects T_PAAMAYIM_NEKUDOTAYIM?Farlie
T
1

After looking at codepad:

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

... should be

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.

EDIT

Per PHP documentation page on static keyword,

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).

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;
}
Tautog answered 14/2, 2011 at 18:10 Comment(3)
Oops, that was a typo! Still getting the error though (codepad.org/wpT0g3VH).Milore
Interestingly, my dev server (php 5.2.7) calls the original error a "Parse error: parse error" rather than trotting out the old "T_PAAMAYIM_NEKUDOTAYIM" chestnut.Tautog
Actually, you gave me a very good tip: codepad.org/Y9ntbREa. Thank you! =)Milore
B
3

The reason for the error is simply that the syntax isn't supported in < 5.3.

However, if you're trying to just access the static variable $id, then the syntax would be:

$result::id

If you do need to access a static variable variable, then a workaround is to use reflection:

$class = new ReflectionClass($xpto);
echo $class->setStaticPropertyValue ('id', strval($xpto));

ReflectionClass

Burma answered 14/2, 2011 at 18:17 Comment(3)
Auch, that is a PITA... $id in this case is not a variable variable. Do you know where I can read more about the support of this syntax? I've noticed that non-static properties also don't work (codepad.org/Fb1mQOvx), is this also because the syntax not being supported?Milore
You're getting the new error because you're using $this in a static function. php.net/manual/en/language.oop5.static.phpBurma
Oh yes, forgot about that! Thank you.Milore
T
1

After looking at codepad:

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

... should be

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.

EDIT

Per PHP documentation page on static keyword,

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).

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;
}
Tautog answered 14/2, 2011 at 18:10 Comment(3)
Oops, that was a typo! Still getting the error though (codepad.org/wpT0g3VH).Milore
Interestingly, my dev server (php 5.2.7) calls the original error a "Parse error: parse error" rather than trotting out the old "T_PAAMAYIM_NEKUDOTAYIM" chestnut.Tautog
Actually, you gave me a very good tip: codepad.org/Y9ntbREa. Thank you! =)Milore
R
0

PHP Version 5.3.3, I am not getting any errors on that code.

Output:

string(0) "" string(3) "dev" string(4) "prod" string(0) ""

Your error likely lies elsewhere. Please double check the reported line numbers.

Reedbuck answered 14/2, 2011 at 18:5 Comment(3)
PHP 5.3 works fine as I've stated in my answer, the problem only exists on prior versions of PHP. Check the Codepad link for an example.Milore
My apologies, I thought you said it worked in prior versions, but in 5.3 it was failing.Reedbuck
No problem, I should have made my question more clear. Thank you.Milore
B
0

I came here by the reference: Syntax error in PHP 5.2 where Chandresh mentioned your link: how ever one work around for PHP 5.2 is:

class Sample{
    public static $name;

    public function __construct(){
        self::$name = "User 1";
    }
}

$sample = new Sample();
$class = 'Sample';
$name = 'name';
$val_name = "";
$str = '$class::$$name';
eval("\$val_name = \"$str\";");
//echo $val_name."<br>";
eval("\$name = $val_name;");
echo $name;

Ignore if you already resolved. Thank you

Beta answered 14/10, 2011 at 9:22 Comment(0)
C
0

Another case :

It may happen on some servers (PHP VERSION ??) if you use: if (empty(NAME_OF_A_CONSTANT)) ...

Casto answered 20/7, 2015 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.