Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77676 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 478 invoked from network); 26 Sep 2014 13:41:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Sep 2014 13:41:46 -0000 Authentication-Results: pb1.pair.com header.from=johnpatrick.gerdeman@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=johnpatrick.gerdeman@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.180 as permitted sender) X-PHP-List-Original-Sender: johnpatrick.gerdeman@gmail.com X-Host-Fingerprint: 209.85.220.180 mail-vc0-f180.google.com Received: from [209.85.220.180] ([209.85.220.180:57508] helo=mail-vc0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 99/61-27807-91D65245 for ; Fri, 26 Sep 2014 09:41:46 -0400 Received: by mail-vc0-f180.google.com with SMTP id hq12so1910428vcb.11 for ; Fri, 26 Sep 2014 06:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=lPE4uc7o6Uthc7G6PZlHcQKJC5YemvtU35OKlrj5kaY=; b=ZXP1XOkeSSjtx/M2C43Gz9Pb6Am63mLCvrJEfT4n56VjFi9rAQj3R7LfFGv9hK3JDB hSAV4yVmpn6/7/8x0rQEV7I3DFRn7PYcIETLBXku0KHSvMLamOEvO4//JakybvLynN0D t81adcK63qFLXfOWYIjDLwV/Au/BI80ZQoyBLs2o16yHRwH86s4GAY6p9ghTXOrSYv2B ASdokGd+/0yi0BFJUQeeS8T0PsTejSys2buYlVov3k2DnAkxqV24hskW/lfvVniyqecA jeNOriRRS6aCcPx/obZBelw6Rym7beR813GoS3U5o/0l/Z99FVNRdiHE3pe8wtNrFz+Z Z2EQ== X-Received: by 10.53.13.227 with SMTP id fb3mr13532027vdd.18.1411738903389; Fri, 26 Sep 2014 06:41:43 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.111.133 with HTTP; Fri, 26 Sep 2014 06:41:23 -0700 (PDT) Date: Fri, 26 Sep 2014 15:41:23 +0200 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=001a1133c43ada2c150503f81335 Subject: Re: [PHP-DEV] Invokation on __toString() for object used as array key From: johnpatrick.gerdeman@gmail.com (John Patrick Gerdeman) --001a1133c43ada2c150503f81335 Content-Type: text/plain; charset=UTF-8 Hi! IMHO defining the value used for the array key should be provided by the user, like Lester Caine said. Using a state or location based hash behaves very unexpectedly and I doubt it would be used very much because of that. Regarding __toString vs. __hash. It depends on what __toString really is for. Now that there is a dedicated __debugInfo method, should __toString enable an object to be used as a string without exception? In this case there is no need for a separate method. Having a separate method might actually be confusing. "When a string is expected __toString is called, except when ..." If that is not the purpose of __toString() than a separate method makes more sense. I agree with johannes that __toString is often used for debugging. I do that quite a bit. However since this would be new functionality there is no BC break and users can be informed before using it. I tried to summarize the four approaches, just to make sure I understood them correctly. Using __toString() ------------------------ toString not set will lead all instances to use the same (empty) array key thus overwriting each other. toString is often used to generate a string represantation of the class. Imagine an emailClass, where toString return the email body. This would lead to huge unwieldy array keys, though technically fine. toString then has three jobs: 1. Output for debug (even though there is a new function for that, it is still often used for that) 2. Textual representation, like an email-body or the content of a stream. 3. array key Currently the following error is thrown, when no __toString function is defined "Fatal error: Object of class O could not be converted to string". If it read "Fatal error: Object of class O could not be converted to string. Expected method __toString()." It would point users in the right direction. new magic method __hash -------------------------------------- This relieves the ambiguity of __toString. It also enables the user to define a key that is sensible for his use case, for example a user id or database id. Currently it issues a warning "Warning: Illegal offset type". If a new magicc method is implemented than it should read "Warning: Illegal offset type object. Object has no method __hash()" state based Hash ----------------------- A state based hash can be made somewhat readable, but changing state changes the hash. $a = new MyClass(); $a->foo = 'bar'; $d = array( $a => 'It Works'); $a->foo = 'buzz'; echo $d[$a]; // Notice: Undefined index On the other hand this would work (Imagine storing something in the session or database and trying to access it from a different instance or from a later request) $a = new MyClass(); $a->foo = 'bar'; $d = array( $a => 'It Works'); $b = serialize($a); $a = unserialie($b); echo $d[$a]; // It Works! internal pointer based Hash ------------------------------------ Changing the state of the object would not change its hash so this works: $a = new MyClass(); $a->foo = 'bar'; $d = array( $a => 'It Works'); $a->foo = 'buzz'; echo $d[$a]; // It works But this would not work $a = new MyClass(); $a->foo = 'bar'; $d = array( $a => 'It Works); $b = serialize($a); $a = unserialie($b); echo $d[$a]; // Notice: Undefined index -- john --001a1133c43ada2c150503f81335--