Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64568 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21776 invoked from network); 5 Jan 2013 23:29:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Jan 2013 23:29:14 -0000 Authentication-Results: pb1.pair.com header.from=kris.craig@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=kris.craig@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.182 as permitted sender) X-PHP-List-Original-Sender: kris.craig@gmail.com X-Host-Fingerprint: 209.85.216.182 mail-qc0-f182.google.com Received: from [209.85.216.182] ([209.85.216.182:53806] helo=mail-qc0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 87/DA-62408-847B8E05 for ; Sat, 05 Jan 2013 18:29:13 -0500 Received: by mail-qc0-f182.google.com with SMTP id k19so10396252qcs.27 for ; Sat, 05 Jan 2013 15:29:10 -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=UJQNL/770mXfozr+SrPlP5oaXSjvMm2JPv769lY2P/4=; b=lUqTPLKq2BbVx/f0FBEGf1bkKC236FRx7P+fCKN8kMPBDEBk2rJ6HKHByauUDPBmXW ohmJ7WSPaddShH35soi/xMAlDy726y6y+Qt+5mZUexRpJFOEoI/NTEx6WrTS2e//sdP7 IK4w8x3fvH2na9L0ZBbjXgNVqCmMg0eYS5h8egJnmL6scl8WeL51klpTkhAaX8h6Wc+d Z5e0mqBugGaafPp4zkSomMlIK3Eam8f8ybHHeLYAsCVk1m45eEhPm9FX+FMEd6woUUW9 Fc89037e+28kkEZ1c4+kZtudk01CwCXGyndE2I+B31vGiKOm+hf+uVo5j4rPmg789EBt OQ2A== MIME-Version: 1.0 Received: by 10.224.188.13 with SMTP id cy13mr35725913qab.53.1357428550023; Sat, 05 Jan 2013 15:29:10 -0800 (PST) Received: by 10.49.64.199 with HTTP; Sat, 5 Jan 2013 15:29:09 -0800 (PST) In-Reply-To: <50E8ADF5.6070504@sugarcrm.com> References: <50E8ADF5.6070504@sugarcrm.com> Date: Sat, 5 Jan 2013 15:29:09 -0800 Message-ID: To: Stas Malyshev Cc: Nikita Nefedov , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=485b397dd3b5883b4704d292f696 Subject: Re: [PHP-DEV] Ruby's symbols From: kris.craig@gmail.com (Kris Craig) --485b397dd3b5883b4704d292f696 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jan 5, 2013 at 2:49 PM, Stas Malyshev wrote: > Hi! > > > I know I shouldn't write "Ruby" in the subject of a letter for > > php-internals ML, but... Just wanted to ask, is anybody interested in > this > > feature in PHP? > > As I understand, :foo is basically an interned string "foo". But in PHP > all constant strings are interned now, so the only thing we gain is > comparison speed. Given that the strings we're talking about is under 10 > chars, performance gain from this would be negligible, and symbols > aren't frequently directly compared anyway. So what exactly we're > gaining that is not there in just constant string? > > > $foo = array(); > > $foo[:bar] = "qwerty"; > > > > var_dump($foo); > > // array(1) { > > // [0] => > > // string(6) "qwerty" > > //} > > Here's a problem. How we know that :bar is 0? What if we serialized $foo > and later loaded it back - would :bar still be 0? Would :bar be 0 in all > scripts forever - and if not, how would we know where $foo[:bar] is > actually stored when we combine two scripts? I think it can not work > this way since link between :bar and actual hash key should be stable. > > > I will drop the part about new variable type, the main thing is how > > HashTables could work with symbols as keys. > > HashTable can accept only string or number as key. As we have seen > before, making number from :bar is very hard to pull off properly. And > if we use string "bar", why not just use string "bar"? > > > 1. More convenient way to use it almost everywhere as a replacement for > > strings and sometimes for constants. There's a lot of code that uses > > arrays as a parameter-stores. For example, here's how you usually define > a > > form in Symfony2: > > How it's more convenient? I don't see any difference. > > > 2. Memory usage reduction. AFAIK, there's a lot of cases like the above > > and the use of symbols would affect on memory usage significantly. > > I don't see any memory usage reduction - you still have to store the > string. Could you explain how memory usage is reduced? > > -- > Stanislav Malyshev, Software Architect > SugarCRM: http://www.sugarcrm.com/ > (408)454-6900 ext. 227 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > 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: Now, if we had symbols, I could get rid of the constants and just do this instead: 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 --485b397dd3b5883b4704d292f696--