Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75124 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59877 invoked from network); 28 Jun 2014 14:02:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jun 2014 14:02:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=mails@thomasbley.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=mails@thomasbley.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain thomasbley.de from 85.13.137.24 cause and error) X-PHP-List-Original-Sender: mails@thomasbley.de X-Host-Fingerprint: 85.13.137.24 dd15934.kasserver.com Linux 2.6 Received: from [85.13.137.24] ([85.13.137.24:52246] helo=dd15934.kasserver.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E4/31-52706-0FACEA35 for ; Sat, 28 Jun 2014 10:02:25 -0400 Received: from dd15934.kasserver.com (dd0800.kasserver.com [85.13.143.204]) by dd15934.kasserver.com (Postfix) with ESMTPSA id 69C912618E2; Sat, 28 Jun 2014 16:02:21 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SenderIP: 197.134.127.203 User-Agent: ALL-INKL Webmail 2.11 mobile To: internals@lists.php.net, sebastian@php.net Message-ID: <20140628140221.69C912618E2@dd15934.kasserver.com> Date: Sat, 28 Jun 2014 16:02:21 +0200 (CEST) Subject: Re: [PHP-DEV] Revisiting Anonymous Classes From: mails@thomasbley.de ("Thomas Bley") Hello Sebastian, for syntax consistency with anonymous functions I would prefer a longer Syntax: $greet = function ($name) {     printf("Hello %s\r\n", $name); }; $greet('World'); $log = class { public function foo() {… }; $logObj = new $log(); and: $log = class extends DefaultLog {… Regards Thomas Sebastian Bergmann schrieb am 28. Juni 2014 15:36: > In a unit test, test doubles (dummies, fakes, stubs, spys, and mocks) > are used to simulate the behavior of complex, real objects when a > real object is difficult or impossible to incorporate into a unit > test. A test double can be used anywhere in the program where the > program expects an object of the original class. > > Traditionally, frameworks and libraries are used to automatically > generate test double objects based on the original objects using > reflection. This idea stems from a time when IDEs were not powerful > enough to do this generation for us. > > Quoting Robert C. Martin [1]: > > "Stubs and spies are very easy to write. My IDE makes it trivial. > I just point at the interface and tell the IDE to implement it. > Voila! It gives me a dummy. Then I just make a simple modification > and turn it into a stub or a spy. [...] > > I don't like the strange syntax of mocking tools, and the > complications they add to my setups. I find writing my own test > doubles to be simpler in most cases." > > The more I think about this (especially in the aftermath of the > unserialize() issue), the more it appeals to me to have "real > code", generated in parts by my IDE, for my test doubles. > > The question remains, though, where to put this code. Obviously > it belongs with the test suite. But how to name the test double > classes? Unique names are required to be able to have multiple > (differently configured) test doubles for the same original > class. Mocking tools (such as PHPUnit_MockObject, for instance) > use random names for their test double classes. > > Anonymous classes could be a solution for this problem. > > I was surprised to find [2] (and even more so because I voted "No" > on it ... and had no recollection of reading the RFC or voting > on it). Most likely I did not see a use case for anonymous classes > back then. > > I would like to propose the following syntax for declaring an > anonymous class that does not implement an interface or extend > a class: > > $o = new { > public function method() { > // ... > } > > // ... > }; > > An anonymous class can extend one class or implement one interface. > Unlike non-anonymous classes, an anonymous class cannot do both. > In other words, it cannot both extend a class and implement an > interface, nor can it implement more than one interface. > > The syntax to have an anonymous class implement an interface or > extend a class is the same: > > interface AnInterface { > public function method(); > } > > $o = new AnInterface { > public function method() { > // ... > } > } > > > class AClass { > public function method() > { > // ... > } > } > > $o = new AClass { > // ... > } > > The ReflectionClass and ReflectionObject classes need to be adapted > for anonymous classes. Only new syntax elements would be added to > the PHP language. Existing elements of the PHP language syntax would > not be changed. No new reserved words would be added to the PHP > language. There should not be an impact on backwards compatibility. > > In case there is enough positive feedback to the syntax proposed in > this email I would gladly write a formal RFC. I could also help with > writing tests and documentation. Unfortunately I cannot implement the > feature myself. > > -- > [1] http://blog.8thlight.com/uncle-bob/2014/05/14/TheLittleMocker.html > [2] https://wiki.php.net/rfc/anonymous_classes > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >