Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104391 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78408 invoked from network); 13 Feb 2019 20:40:25 -0000 Received: from unknown (HELO mout.gmx.net) (212.227.15.19) by pb1.pair.com with SMTP; 13 Feb 2019 20:40:25 -0000 Received: from [192.168.2.104] ([79.222.45.201]) by mail.gmx.com (mrgmx001 [212.227.17.190]) with ESMTPSA (Nemesis) id 0MQRWm-1gUeS43frA-00Tmbu; Wed, 13 Feb 2019 18:23:33 +0100 To: Rasmus Schultz , PHP internals References: Message-ID: <16eb6670-f65a-a090-e17e-44554ba115be@gmx.de> Date: Wed, 13 Feb 2019 18:23:25 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:nfna5gOz9WVoamVtaRy0j+dGTobPYEjt8pJgh893V1HyllFuhW5 op0DrrIlalqUqgI4ZMkjRRf6hSOnsaRZCHWHoVftOr+y8Lwr+ZJO2lNYwyE+ZxU8ouX+dxD hCzUoW9oyCpA4wsQ00KIFODxGp2IMZTmnA5zRCLU026Z3jmRcGPWmEdNnwqIp54nWnAQQmi QqcfqYoUneX+qMgqfBVTg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:uHArPPZImj8=:kQsNh6F2pKpWyQM9EYpx5Y Aw/ktxm8EOZvTow7Rcl5xE1A6fQYzmJ8HdXWamempr7LITnzfKK9wdGXOlBXf5XRK/hJaCmqb BkF7iwpDtZpcc2xKB0FwMpWKLi8iJgLsIrhL9G9ehkuaYYjMmo1bV51IUUKevtyiAwhm5vAKU qpcn83CrYZmPu7SayWnf+GkD6U5Xi+ZiDpEwGkc7inZHpzVkB4crJZgsE059881aVjdiT4uRg BaX9/rQELZtIkBFhN+KXLx6/n4KUU0fyOkI0eHBRbm9ScIJxowvIOOPoAAAvbuzGu038K2EGt uQeCxz7iZ+JMPsrapVLOeyWBCqpQ4rebMZ30X7+huo19pYZWmoUrE7Ppy80YfwEovtHgdv6kz 4Q9cnjUPIID801assYeVF6J2b26a2dq3t1vlRQrRWaWEgDHKtzVNxJt4gD5LrTF9nKJaytawB FIvP0+36AJgOchNgjbWEBJbsxoLjafqp1rgoXI25jqs+APTXb4usLZ+aeGInJFVO9tvYxe6hf Us8yEOwflJK4oPv+NHQrzws/NYSbImUInyScF8zM6V9u0aRkKBS6D84W06apxALxPQJLfipiU 2hubEgf4qAFf5WrK79qJLDX6BP/ShI0LL/2l9EDSiw4x9TP2QMR2FvkK/x07D6kYJN6FwveiQ PJ51fRVuTYAm1ZZ5A0GgoN95VfjcKo6KRNCjOM28riq23fKsfO7Eok4IHe66NcQZONk0vaHOr KmGfk2v2fyrV1qlu0z08Ey1Y6vm2TjGLU7bG2ZifASwRS8D7P1FYze0gXvYgVVZAEM9+99Zlr EiU9KZRRE9eIgrHFHyVu5gWtp5MnQ317WheFJ9Uqf14iNEAfeQVj+SIYcHrJp6Q2yOaQZe13x n/toHBCzmEAMMh6vQSYjOPt0GABKP3/PQrlqAzDQzYKDc0rJ4Bc5JLAg4Wmhe8WEBMalcBZ2A o9GGxN+Lp0w== Subject: Re: proper anonymous classes? From: cmbecker69@gmx.de ("Christoph M. Becker") On 13.02.2019 at 09:01, Rasmus Schultz wrote: > The fact that the anonymous class syntax defines a class *and* immediately > constructs an instance is quite annoying. > > For one, this is quite incompatible with DI containers' ability to resolve > constructor arguments. > > Lets say you DI container can register a named component by merely > referencing a class that uses constructor injection - so lets say this > works: > > class MyController { > public function __construct(MyService $service) { > // ... > } > } > > $container->register("my-controller", MyController::class); > > Now I want to register an anonymous class, for example as part of an > integration test-suite, which is common enough: > > $container->register("my-controller", new class { > public function __construct(MyService $service) { > // ... > } > }); > > This doesn't work, because you're expected to actually pass the constructor > arguments immediately - because you can only define an anonymous class > while immediately creating an instance. > > What I really want is just an anonymous class - not an instance, so: > > $container->register("my-controller", class { > public function __construct(MyService $service) { > // ... > } > }); > > The question is, what would a class expression without the new keyword > evaluate to? > > Since we normally reference classes with just a class-name, I guess I'd > expect a string, like you'd get from the ::class constant. > > Any hope for something like that? Not particularly efficient, but likely sufficient for the use case: $container->register("my-controller", get_class(new class { public function __construct(MyService $service) { // ... } })); -- Christoph M. Becker