Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104390 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55086 invoked from network); 13 Feb 2019 19:02:56 -0000 Received: from unknown (HELO mail-qt1-f180.google.com) (209.85.160.180) by pb1.pair.com with SMTP; 13 Feb 2019 19:02:56 -0000 Received: by mail-qt1-f180.google.com with SMTP id p48so3082446qtk.2 for ; Wed, 13 Feb 2019 07:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UHf2DQItOKI8ETphzKKq4M+rc/+fJMwJctEyjWobLjk=; b=POxjblJB48Ra9AdzR/mBKQZd0tEX6k62oJ4AumMYENAKKY4Rp10zbMkHh82V3dqG/v sM7zBSbMm4Vj6eNj3N+6VmuT6xxE4otTXmSRGTTqorEkHApI/ZII76O1bxafaBOHizGB khlWKWUNTebByPc+ptlKZS66hcOgovbfiJaUMA3B1qioUQG9ArozFD+mwWzYPm0LyvDE sMt1d2GWhrCA53hVBs3KeumPu9gCm4TUZXEMmfjsFZfVfh7KuQlbY659mumHkqndi60D W98IYNZskTLrrt9r6c4aJ6qNzNhHqv6V0jdQU0Q3XDOMCSOO+5xAhgfauaVlIflueUl6 H1qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UHf2DQItOKI8ETphzKKq4M+rc/+fJMwJctEyjWobLjk=; b=s1n/BKJCwsbWGI8Kb2AOZi4tR0xPC/1nTgYRy2KWstlZgp2YlgavvqMWqtPMfCrvHG kE9Pf6JdiRLAD6Ei+IOLJg4YCYzpECzuxbPwiSNH3Lyp5B0I8y6OJd5DZMx5MR9lXr6b bjW+b9TSnohk9ZS6SJMwfyEoBe8kXUSdRAIy0etIKw5b+afFcYMEeQYtljN3jZb65oZm UyY9pBQW/sRDsrWlR3KzqPFiUgCjFz1JbigtycyFeU0UxuDUYSUQBGLQZkl/Z6Mm11jS Zelkg97QzZcZREdQAOiaeapHxVKABEtmrKoHIXnIffxZpS2MdCT7NStPVe8cF5+7jm1A pd9A== X-Gm-Message-State: AHQUAuYsQaGnM3cfB/q9Smv7J/LWSRDZ2Eg451bHHcYebV+slyWP4pKe mvx4BLWRNa+FK+epgTg0XzkplqC8ZkYL/bX0/AZzbA== X-Google-Smtp-Source: AHgI3IY+J/SF7jrQE0GgXomwEKfyXIFhIryn0SsNlwog62BpfWi+tHvPFQB2jr8AitkACS8PgGRInsRE1XDMrushunU= X-Received: by 2002:a0c:93ab:: with SMTP id f40mr435027qvf.59.1550072766056; Wed, 13 Feb 2019 07:46:06 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 13 Feb 2019 16:46:00 +0100 Message-ID: To: Joe Watkins Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000098de310581c8708c" Subject: Re: [PHP-DEV] proper anonymous classes? From: rasmus@mindplay.dk (Rasmus Schultz) --00000000000098de310581c8708c Content-Type: text/plain; charset="UTF-8" It doesn't have to be? It could just be an inline class expression (without the new keyword) that evaluates to a string, e.g. a machine-generated class-name? I don't think that's what first-class support means? e.g. nothing like constructors in JS or class instances like Dart? This could just be a "cheap hack" with machine-generated class-names, so we can reference anonymous classes by name, the same way we reference classes now. Introducing classes as instances of some kind of "class class" would be a more drastic change, since this would not be compatible with existing string type-hints for class-names. (?) On Wed, Feb 13, 2019 at 9:16 AM Joe Watkins wrote: > What you describe is first class support for classes, nothing much to do > with anonymous classes. > > On Wed, 13 Feb 2019, 09:01 Rasmus Schultz >> 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? >> > --00000000000098de310581c8708c--