#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Why underscore after dollar sign in a variable?


    Why does this book keep adding underscore after $ like
    PHP Code:
    protected $_name 'book'
    instead of just
    PHP Code:
    protected $name 'book'
    ?
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,102
    Rep Power
    1990
    it's an older-style way of signifying a variable as protected or private in PHP4 when there was no differentiation between public, protected and private like there is in PHP5. It's a short-hand way of telling future developers that that variable or function shouldn't be used outside of it's current context. Of course in practice that never stopped anyone...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0
    It might confuse you with system variables like $_GET['something']
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    I still mark private and protected variables with an underscore, it's immensely helpful when reading through the class's definition. I don't want to have to scroll up to see if $this->property is private, I can tell just from the fact that it's called $this->property and not $this->_property.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,102
    Rep Power
    1990
    Originally Posted by ManiacDan
    I still mark private and protected variables with an underscore
    Me too. It may be a left-over from PHP4, but it does make things a whole lot more readable when someone is trying to see what someone before you has done. I know that when I go back over something that I did 6 months ago I need all the clues that I can get.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    I know people who use hungarian notation in PHP, even though it's loosely typed. Something you do in code doesn't have to be for the compiler/interpreter, it can be for your future self (or the person who takes over your code in 3 years).
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo