Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123642 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 313241A009C for ; Sun, 16 Jun 2024 17:54:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718560529; bh=PiJuczdvKwXqHanhcqCn+cDMsdbr8glm+WGwo9LnkyI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=NX8IOZEDhZ1KDzugLWUU90KhD45M44Jcwb03xjdX6MMaCMPiA60BoAfZrAcDVAeLF InycsjYV7CNdI1TFLp9ChFh8UGKXIxozq27u9Jg9APJo0R5vtjidiQO+qXMqvWP43Y k+ZVtLf6DSy/zkC4owQfInNY3p2Sz8YNl8g6oRYD+AWrT4wqofAI+F+CuPCBdpi7dS Nur5Qi70CiuQIPQzlnSnudz7vQuXQmC66Va7fZSh3BJrr5xI8M8UIeFYkeXQx1ESat E60vva/iEtJn7QIJ7a53O/UfiEBaULJHcl7YXtMQ7iuOK7j3RtgKVl8jY8aZfD2xf6 T2xLxaRo8kt8A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2FEFF1807F6 for ; Sun, 16 Jun 2024 17:55:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 16 Jun 2024 17:55:24 +0000 (UTC) Received: by mail-yb1-f179.google.com with SMTP id 3f1490d57ef6-dff17fd97b3so2424658276.2 for ; Sun, 16 Jun 2024 10:54:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1718560452; x=1719165252; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=3Whfvw1RScF9+qONKtUD+LmifBB4CjDdriApzSQV18w=; b=fFnmnq75U1QwT3Re5j9bnCpptaJbYmwNdS3Tw1JU0+lHC3ZsRoBNVAtNt4TyVgEsBQ 37eIA4ueFDYZEjJ1fPkgWW+ejfBoF+BP75LCU4qKe/vIMuNnYT5m3B7LRZjqZXfvOaEU hRusT3R01t0ZsnoxQtq5weov9NlLD8dDYF/Jya32ZSurWwbPF8y1mCCgW3XYjeNBp0ZZ 5luTTTvWutycQfoCvyBPogoF4bOl0EkdpOXFT5z/YxTwvv3/SMT1YDLtA0HiAxvajYsw cMLJDymRjmtrIB/i5dhemTkNP05wlCn0wSqHsaaXFfaNR9DwL5XgD5ljRVI8nExf7xQH 1eew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718560452; x=1719165252; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3Whfvw1RScF9+qONKtUD+LmifBB4CjDdriApzSQV18w=; b=j/M671gJKRDnv0QtKjmFEojVz8xqkBVYKpwphOncVtS30cAnvfbGF3iGmfn6cjL0au Wnp6aYQMbS2JWb3hiV9KpKOz1kAcPrf8Pm4n+Pd6OKyxlCEtShOaDpOIZZ/LpQDI/mmT Mav8LNYof0rdPx2rG/e65Vksh6EDonFDg/J72FzEvxWcxeen524kTvF0raQzsKQEtEpc qMgM3BwEU6Nj70ogc1jZVz4ZFMOVzgWsRMPYKoPyibfUalXZ65RH70L0+/NIhg9Reuap tXM2SnQ0TsVJXqycBBzobAlNFL2nvOMgzM7+sg6Kmu7iIyv23s1GPhEfbTV6LQjdOTmY 5Eww== X-Gm-Message-State: AOJu0YwH4+Iugi/0PRx+dKmXRbe9oSdbBHDCKEv4m7JnxL+y6BG19zOj EKJdO2dED4raWHz+AnyOersofhWz+cwEFiqlT/AW26piEek6OkeXgCEBTKhotapuH0qpmbvb8ot nDrkWJB5Z7aBXMTxMWFnx3weBlH7zzDk5tQlFuqesOMHpGWRfhajCrw== X-Google-Smtp-Source: AGHT+IHkKM8E9MRCV8wFU/lj36tCQ7pB3XhJOx7I5S5LFXRNu7PRqKtgx8NWzLdzZhllZFvUGnW119k4kqJ5m51zHT8= X-Received: by 2002:a05:6902:1021:b0:dfe:5f44:d46c with SMTP id 3f1490d57ef6-dff153eb893mr8542863276.36.1718560452571; Sun, 16 Jun 2024 10:54:12 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <0cf69a14-f1b5-4077-9d91-d7b579485eec@scriptfusion.com> <936e1aa3-48cc-4552-9f68-676ebcdeb596@rwec.co.uk> <1fc2f2d0-718f-45ec-8968-b66a1bde686e@scriptfusion.com> <4e439377-5c20-40d7-bb99-5d0ac6c348e9@rwec.co.uk> In-Reply-To: <4e439377-5c20-40d7-bb99-5d0ac6c348e9@rwec.co.uk> Date: Sun, 16 Jun 2024 19:54:01 +0200 Message-ID: Subject: Re: [PHP-DEV] Static class To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" From: andreas@dqxtech.net (Andreas Hennings) On Sun, 16 Jun 2024 at 19:09, Rowan Tommins [IMSoP] wrote: > > On 16/06/2024 16:24, Andreas Hennings wrote: > > For a function call, the namespace is usually only visible in the > imports list at the top of the file. > > > This is one of those interesting cases where the language itself is flexible, but people are used to a specific style: a use statement can import any level of namespace, to provide as much or as little context as needed in a particular piece of code. > > namespace Acme\Artistry\Color { > function fromRgb($red, $green, $blue) { /*...*/ } > } > > namespace My\Own\Application { > use Acme\Artistry\Color; > > // ... > $color = Color\fromRgb(1,2,3); > // ... > } > > > Maybe that's rare in practice, but it's always tricky to judge how much the language should account for such habits - see also "every class needs a new file", and my pet hate "deprecation notices get promoted to errors". Yes, the possibility for namespace part imports exists. Unfortunately these namespace imports usually need to be set up manually, whereas the regular class or function import has better automatic support from the IDE. Also, namespace fragments are often more generic than function names, so more likely to need a custom alias. > > > The class name that is part of a static method call hints at an object > type, whereas for a namespace fragment it may not be really clear what > it describes. > In a static factory call, like `Color::fromRgb($red, $green, $blue)`, > I would argue that having the class name as part of the call improves > DX. > > > This example seems slightly off: if the static method is on the class it is a factory for, that class clearly isn't completely static. There are two separate argument tracks here. The `Color::fromRgb($red, $green, $blue)` has a nice DX, and justifies the eixstence of static methods in general. This also aligns with Larry's argument about "named constructors", or the more general "type context". Then, you may rename the instantiable part of the class, while leaving the static factories in place. You end up with an all-static class. Or you already start with different implementation classes like RgbColor and HexColor, all implementing ColorInterface, but you want one centralized place for static factories. I personally prefer to call this `Color` instead of `ColorFactory`. For me `ColorFactory::create()` would give me a ColorFactoryInterface object, not a ColorInterface object. > > Presumably what we're actually talking about is something like "ColorFactory::fromRgb(...)" - or maybe "GraphicsFactory::colorFromRgb(...)", Basically yes. > where you might well want to abbreviate to "colorFromRgb(...)". I assume you mean as a procedural function? Technically that would be possible. But I still like to keep the `Color::` part which is suggestive of the `ColorInterface`, even when the class `Color` itself is not really instantiable. A namespace fragment or a function prefix is not as suggestive that this produces a ColorInterface object > > > Your points are generally well made, I just wanted to point out there are some nuances in that particular area. Thanks.