SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Serberus's Avatar
    Join Date
    Oct 2005
    Location
    Herts, UK
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP5 class constants in switch cases

    I just tried to do this and got a parse error:

    PHP Code:
    class Blah {

        const 
    FORMAT_MP3 200;
        const 
    FORMAT_WMA 201;

    public function 
    fetchUrl(...) {

        switch (
    $this->getFormat())
            case 
    self::FORMAT_WMA: <-- line 32
                
                
    break;
            case 
    self::FORMAT_MP3:
                
                break;
            default:
                
    // Unrecognised format
        
    }

    Code:
    Parse error: syntax error, unexpected T_CASE, expecting ':' or '{' on line 32
    Any recommendations on how to use class constants as cases in a switch?

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well from what the documentation states :
    http://us.php.net/manual/en/language...nstants.syntax

    Constants may only be defined using the define() function, not by simple assignment;
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the error message is actually pretty clear:
    Parse error: syntax error, unexpected T_CASE, expecting ':' or '{' on line 32
    Looks like you're missing a {.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    switch ($this->getFormat()) { 
    Quote Originally Posted by byron3@earthlink View Post
    Well from what the documentation states :
    http://us.php.net/manual/en/language...nstants.syntax

    Constants may only be defined using the define() function, not by simple assignment;
    He is using class constants: http://www.php.net/manual/en/languag....constants.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Zealot Serberus's Avatar
    Join Date
    Oct 2005
    Location
    Herts, UK
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sorccu View Post
    Well, the error message is actually pretty clear:

    Looks like you're missing a {.
    Whoops


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
  •