SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    declare class variable: number

    Hello,

    I know this is usually not a good practice, but I'm trying to declare a variable named "302", and it really makes sense in the context of my class.
    PHP Code:
    protected ${"302"}; 
    This obviously doesn't work. I've tried a few combinations, without success.

    Anyone knows how to do it?

    Best.

    -jj.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    You dont. Variables in PHP must start with either a letter or an underscore.

    Quote Originally Posted by PHP Manual
    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I was poking around with const and define() and I discovered this:

    PHP Code:
    define("302""My 302 message");

    echo 
    302 PHP_EOL;

    if( 
    defined("302") ) { 
    echo 
    'well it is defined ... somewhere at least... ' .PHP_EOL ;
    echo 
    constant("302") . PHP_EOL;

    Gives:

    302
    well it is defined ... somewhere at least...
    My 302 message

    But I cannot replicate it in a class using const, you have to precede it with a letter or _ etc.

    How are these representations in you class so useful? Doing redirects?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    echo 302 . PHP_EOL;

    Wouldnt that fail because of the concatenation operator casting the 302 as a string to concatenate it to "\n" (or whatever PHP_EOL evaluates to)?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    echo 302 . PHP_EOL;

    Wouldnt that fail because of the concatenation operator casting the 302 as a string to concatenate it to "\n" (or whatever PHP_EOL evaluates to)?
    It did. It cast the constant 302 by variable name to "302" and appended the PHP_EOL to the end.

    I agree with everything stated at this point, that your best bet is using _302 and using getters and setters to work with the value.

    PHP Code:
    public function Get302()
    {
      return 
    $this->_302;
    }

    public function 
    Set302($value)
    {
      
    // do any validation that may be necessary
      
    $this->_302 $value;

    This at least makes it a bit more readable and easier to remember how to access the variable from a development standpoint.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    echo 302
    without the PHP_EOL still outputs 302 ... so somehow it seems its' abject inability to follow the rules of constant or variable definition therefore cause it not be looked up in the variables table -- even though it somehow circumvented those rules to get itself added into that table.

    Is there are rational explanation? If so I'd like to hear it.

    Whether anyone finds it remarkable, odd, or quirky -- its such an edge case, does it really matter? Probably not.

    Anyhow, I'm still left wondering how the OPs class is using (or intending to use) these vars ...


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
  •