On Sun, Jan 6, 2013 at 12:29 AM, Kris Craig <kris.cr...@gmail.com> wrote:
> I'm still a total newb when it comes to Ruby, but as I understand it, a > symbol can be particularly helpful by maximizing code readability without > sacrificing efficiency. As a PHP guy, I tend to think of a Ruby symbol as > a constant that doesn't need to be defined or set. Its value is set > internally and is not relevant in userland except for the fact that it is > unique and unchanging. > > For example, in current PHP, let's say I just bought a dog: > > <?php > > define( 'BROWN', 0x01 ); > define( 'BLACK', 0x02 ); > define( 'PURPLE', 0x03 ); > > switch ( $color ) > { > case BROWN: > // Brown dog! > break; > case BLACK: > // Black dog! > break; > case PURPLE: > // Purple dog!? > break; > } > > ?> > > Now, if we had symbols, I could get rid of the constants and just do this > instead: > > <?php > > switch ( $color ) > { > case :brown: > // Brown dog! > break; > case :black: > // Black dog! > break; > case :purple: > // Purple dog!? > break; > } > > ?> > > In both cases, we really don't care what the actual values of brown, black, > and purple are. We just want it to be unique so we can reference each of > them in a visually friendly way with minimal performance impact. That's > where I could see a valid use-case for Ruby-like symbols in PHP. > > --Kris > The reason that Ruby needs symbols basically comes down to the fact that Ruby has *mutable* strings. I'm not sure how they came up with that idea (I mean, really, mutable strings? wtf?!), but due to it Ruby needs another string type that is immutable. That's what symbols are. Symbols are Ruby's variant of immutable interned strings. PHP doesn't need this because strings in PHP are immutable by themselves and automatically interned where reasonable. Nikita