SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reference const definition dynamically

    Hello, to keep things simple, I have made an example similar to my actual implementations,
    PHP Code:
    <?php

    class myClass
    {
        const 
    APPLE 1;
        const 
    BANANA 2;
        
    //.....might goes until hundreds of lines
        
    const  PINEAPPLE 1000;
    }


    //let's say user knows the name of fruits
    //so if user input APPLE we should print out the define value
    echo myClass::APPLE// output 1

    //I can use switch statment to refer to the defined constant value in myClass
    $userInput 'PINEAPPLE';

    switch(
    $userInput)
    {
        case 
    'APPLE':
            echo 
    myClass::APPLE;
        break;
        case 
    'BANANA':
            echo 
    myClass::BANANA;
        break;
        
    //....until hundreds of lines
        
    case 'PINEAPPLE':
            echo 
    myClass::PINEAPPLE;
        break;
    }

    //but instead of writing such a long switch statements, can we refer to the defined constant dynamically ?
    //I have tried out this but is an error
    echo myClass::{$userInput};

    ?>
    So is there an alternative way to solve my problem of without using switch statement ? The myClass has to have constant definition. In contrast, not using variable. myClass is a class that we going to use later. Thanks.
    Last edited by eagerlearner; Aug 24, 2007 at 05:56.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I think you need to rethink your system if it need that many constants.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    to keep things simple
    lol.

    As that is an example, are the names actually in a pattern like that?

    If they are in a pattern, you can use simple loops to define and call them.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But the system has to be a constant, it's not my class, more specifically I am using a ParserGenerator class from http://pear.php.net/package/PHP_ParserGenerator
    LexerGenerator http://pear.php.net/package/PHP_LexerGenerator

    So there is not way on doing that ? Thanks

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks arkinstall, the myClass is already coded, so now we are using the myClass, so we cannot simply define it on our own.

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What are the output constants? Are they in such a simple linear pattern?

    In which case, you can use:
    PHP Code:
    if($inpt str_replace("CONST_VAR"""$userInput)){
      echo 
    constant("CONST_VAR"$inpt);

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooo sorry, I just got you, it's not in such simple linear patern, I am going to modified my constant name. lol

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In which case, just do this (although it has no verification):
    PHP Code:
    <?
    echo constant($userInput);
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works but more precisely it should be constant('myClass::'.$userInput).


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
  •