Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64572 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27309 invoked from network); 5 Jan 2013 23:37:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Jan 2013 23:37:55 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.41 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.41 mail-la0-f41.google.com Received: from [209.85.215.41] ([209.85.215.41:60201] helo=mail-la0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9A/1C-62408-259B8E05 for ; Sat, 05 Jan 2013 18:37:55 -0500 Received: by mail-la0-f41.google.com with SMTP id em20so12257041lab.14 for ; Sat, 05 Jan 2013 15:37:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=saX2KdsJnCpb+T4LztsXAVjOfLWHbq7vuw7uBXvbL6k=; b=Y+HDa+pg/1aNQMetajSNkG+uREzw6AERTpBgsf95bmMSahDwNb3M8Ii1fFL83F4n9t yhykQ8t/Vs5VRuxiqCJVyKf6Og+s6eTsygDl1QbzlvcpIHMOl8fwoB2X76OQdmkmYxZ+ jdfBn1Xn8/8vWVfaq4PcKQrBnezOlNW/c4qq8b7Z+osCIyhF0VnMnER3kp8/ON415msE tNmc0G+n+ewFTprdJ69ctQZRFBECdA1SwiG0Gp7xK9SA1RKrJxoiM6Q0ztPogkWLKhd9 zHIWu34nLr9hvK7jMp77cTw0X4VE2Ek5XUxpGiS1ze6jbjJwL2SvRX4bSZvQkA/YWEPc p0UQ== MIME-Version: 1.0 Received: by 10.112.51.175 with SMTP id l15mr22319433lbo.5.1357429071273; Sat, 05 Jan 2013 15:37:51 -0800 (PST) Received: by 10.112.4.168 with HTTP; Sat, 5 Jan 2013 15:37:51 -0800 (PST) In-Reply-To: References: <50E8ADF5.6070504@sugarcrm.com> Date: Sun, 6 Jan 2013 00:37:51 +0100 Message-ID: To: Kris Craig Cc: Stas Malyshev , Nikita Nefedov , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=f46d0401678f99e08004d2931550 Subject: Re: [PHP-DEV] Ruby's symbols From: nikita.ppv@gmail.com (Nikita Popov) --f46d0401678f99e08004d2931550 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jan 6, 2013 at 12:29 AM, Kris Craig 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: > > > 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: > > > 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 --f46d0401678f99e08004d2931550--