SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Newcastle, England
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class::Variable php Parse error: T_PAAMAYIM_NEKUDOTAYIM

    Hi,

    I have a class that contains a static variable:

    Code PHP:
    class Page {
    ...
    static $name = 'Standard Page';
    ...
    }

    I can call it using Page::$name.

    However, I would like to call the static variable using a class name that is stored in a variable.

    i.e.
    Code PHP:
    $class_name = 'Page';
     
    echo $class_name::$name.

    However, when I try to call it this way I receive the following error:

    Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in....

    I'm pretty sure this is hebrew or something and it means unexpected :: or something?!

    Is it possible to call a static variable the above way?

    Thanks in advance

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your syntax is a little out.

    PHP Code:
    <?php
    class Page
    {
        const 
    NAME 'Standard Page';
        
        public static 
    $name 'Standard Page';
    }

    $class 'Page';

    echo 
    $class::NAME#Standard Page

    echo $class::$name#Standard Page
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thejackel View Post
    Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ';' in....

    I'm pretty sure this is hebrew or something and it means unexpected :: or something?!
    Yes: http://en.wikipedia.org/wiki/Scope_r...n_operator#PHP

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Newcastle, England
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thank you for your help.

    SilverBulletUK, my syntax was a bit slack, thanks for that.

    I've tried with your syntax and am still receiving the same error??

    Very odd.

    If I use Page directly when calling the static variable, it works fine.

    It's when I use a variable ($class) to specify the class, that's when it has problems.

  5. #5
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It won't work in PHP < 5.3.

    Here's a workaround:

    PHP Code:
    <?php
    class Page
    {
        public static 
    $name 'Standard Page';

        public function 
    getStaticVar($var) {
            return 
    self::$$var;
        }
    }

    $class 'Page';

    $instance = new $class;

    echo 
    $instance->getStaticVar('name');
    ?>
    or, without instantiating an object:

    PHP Code:
    <?php
    class Page
    {
        public static 
    $name 'Standard Page';

        public static function 
    getStaticVar($var) {
            return 
    self::$$var;
        }
    }

    $class 'Page';

    echo 
    call_user_func(array($class'getStaticVar'), 'name');
    ?>
    Off Topic:


    Why the massive padding at the bottom of code blocks? I left no white-space between ?> and [/PHP]

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    Another workaround is to use reflection (that way, the target class does not have to be modified).

    PHP Code:
    class Page
    {
        const 
    NAME "Standard Constant\n";
        public static 
    $name "Standard Static Property\n";
    }

    $class 'Page';

    $class = new ReflectionClass($class);
    echo 
    $class->getStaticPropertyValue('name');
    echo 
    $class->getConstant('NAME'); 

    Off Topic:

    That extra padding has been an annoyance for a long while, though I figured if someone wanted it removed it would have been done already so it's here to stay.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Newcastle, England
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    genius, thanks decowski!

    I never realised it was due to PHP 5.3

    Thank you to everyone else for your input also

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Newcastle, England
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you also Salathe, that's another useful way achieving the same objective


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •