Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123641 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 1E4321A009C for ; Sun, 16 Jun 2024 17:37:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718559520; bh=7WpsQOVjE02UtaG0oFPo/VYQrRLiwWhGokjRosFAgYI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=UdoMhuWAMJR/XZuPZi7cwPsa431Zx9Jcce4t7wqpkRJ1e3o91I8Xt3XhIWYds+FTX EjHny5l8225kU0PceIPxZ2AklWg2fofRaNY8LaKDMDwetcRBuKPIGpeA3JkvBHphcT VCdv/ypTtB2qNrTPq3//i9tTez63pchB+FQ1k1KldYQXhyCN3k8LaNuNpCWzEq7qwv PEgapjGoBP6f4YBDJPmccjsyg5lTbFBJkDp5hyHLtoSL+gIAu29c3d3FUU4Vgk+Y98 u45iGguIBjlOR+1GVAiJVVHKoyuD0UQQRinBDEINOnjp4UHuXHx1XKgtSMSBv8pK6y 9+AVk2Nv+GCYg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B12A9180718 for ; Sun, 16 Jun 2024 17:38:38 +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-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (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:38:38 +0000 (UTC) Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-dff302847a8so1099375276.0 for ; Sun, 16 Jun 2024 10:37:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech.net; s=google; t=1718559446; x=1719164246; 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=K0y3RAu+EjRtvbUV6l/8JWWAEIOGKOxpsCEqw7rM8As=; b=ma3K47ohAzv6b5UFooyogD4MvywW+x0iuuBen3wR3YQO6pW5VeATmjV3uuLkJt0Qt2 JK8u1q/A7xSJDcu46RsmJCDzgRL40tPnRxB62wO+2OkkNyFcZRrRK7ToxADPQejkiMjw XnA9Sqb7M5StT8CYWddtpdJkOnDl9uLGF9oW+U3IBfXuEPjKz3SXFUfq98r0kght1uvC 9HVn38ZW86qpeh7sSddvbC9yNVpFsxC1iowg7M0KLQ9+FSrwG+iMBElfz+gl0KNTVlCg F9q/8hv5oaW/sRwJaNb7IVCzOn0DS6/CYZxI1WeP42lcYT12lDdzTVdZG5MpK0EQUnWj qKGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718559446; x=1719164246; 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=K0y3RAu+EjRtvbUV6l/8JWWAEIOGKOxpsCEqw7rM8As=; b=V6kst3d+Rp8kL1MG4AxClCjWwMSaJoHCFneKL1NujznBm2hCLvgbWVijyUaRLAwMql E0gTumr4a3GiGHOh1+5U881LLujYyAnUwE1P8htqdO20Sgowd/Is/U8c8mmuD+zxaPju SL6pRwpu2uBr1Q5whc4fufD9R5rPbPeUalYT3gQXFBuOtaTMBCjfjhq4f0ud5bHvf0c6 V9yjVhKHDXkrh4Yu+xOa79QlrUCC96k/kzDxMhyS1SdxEUhPTshXU9zXJjCTjG3k5xtn DFwKVLUyfTQqis1jbmPKSEaYN8YgAi5aJQ1GguZhLw4Kdk+ohHLqLKvxHWKEYtMVlsHC f37A== X-Gm-Message-State: AOJu0Yx0I7XHTkZCQAJ+MPLV6ukkH4lrCbEQP+rXbBhlmAPZkV9WzqUJ QHJll8qDutPmeDWPyo5JikBIAt4kWSod4iuQdYsO4qLLsJmWJrdgBGeeJWR0wEFtKn4MY3OkhpT KKcMQz3r3s5NIrslsgVV2xXlHb4aWwG4EaGEXcFI5r8oIrVR7cQm9jA== X-Google-Smtp-Source: AGHT+IE3l6j5NLey+i4v1pTEe184/Z1xvR/2Z03u5TZUSO5NYUpd7tqBkp+p1XrWcFyF7f7KmmTAGpc1DiTGovHLHsw= X-Received: by 2002:a25:f605:0:b0:dfa:cd11:df34 with SMTP id 3f1490d57ef6-dff15500601mr7613867276.61.1718559446068; Sun, 16 Jun 2024 10:37:26 -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> In-Reply-To: Date: Sun, 16 Jun 2024 19:37:15 +0200 Message-ID: Subject: Re: [PHP-DEV] Static class To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" From: andreas@dqxtech.net (Andreas Hennings) On Sun, 16 Jun 2024 at 17:44, Larry Garfield wrote: > > On Sun, Jun 16, 2024, at 10:24 AM, Andreas Hennings wrote: > > Regarding function autoloading: > > > > A more interesting question to me is a convention where to put each function. > > With classes, PSR-4 tells us exactly what one can expect to find in a > > class file with a given name. > > Perhaps a separate directory tree per package, with one file per sub-namespace? > > Or just a package-wide functions.php? > > > > > > Regarding static methods vs functions. > > > > From the older thread, I can see different levels of possible > > objection to all-static classes: > > 1. "Every static method should instead be either a non-static method, > > or a regular procedural function." > > 2. "Static methods are acceptable, but no class should exist that has > > only static methods." > > 3. "Static methods and all-static classes are acceptable, but no new > > language feature is needed to specifically support all-static > > classes." > > 4. "A new language feature for all-static classes could be acceptable, > > but it should be very minimal and declarative." > > > > I see myself somewhere between 3 and 4. > > For reference, I have documented my stance on statics here: > > https://peakd.com/hive-168588/@crell/cutting-through-the-static > > --Larry Garfield Thanks, Larry! This aligns more or less with your messages from the older thread. > Make it a function. This is fine, but: If you would replace all of your functions with static methods in all-static classes, your code would still be as testable as before. The calls would look more verbose, perhaps more confusing. Developers may find reasons to use static methods and possibly all-static classes over functions: - Conformity with an existing code base or ecosystem. - Lack of a convention on where to put your namespaced methods, and which namespace to use. - Personal habit and familiarity. - Convenience of moving code between static and non-static methods. - The possibility of using private and protected static methods, and static polymorphism (whether that is good or bad). - Easy access to class constants. - The static methods already exist. Besides, the namespace lookup is generally less ambiguous for classes than it is for methods. A `str_starts_with()` call could reference a function `Current\Name\Space\str_starts_with()`, or the global `\str_starts_with()`. A class always needs the explicit alias or a `\\` prefix. At the same time, the top-level namespace is crowded with native procedural functions, but not so much with native classes. At first, this is just an inconvenience when reading the code. But it can also lead to real bugs, if a function alias gets lost when you resolve a merge conflict, and you don't notice it because there is a top-level function with the same name. So, the above would be reasons why developers might not go all the way to namespaced procedural functions. Then, the mere presence and habit of static functions will lead to some cases of all-static classes. Even if you only use static methods as named constructors (static factory method), you can end up with an all-static class, as in my example above: - You start with private constructor + public static factory. - You rename the part of the class that is instantiable, to better distinguish different implementations, but you keep the static methods in place for BC reasons. (this assumes that all dependent packages depend on the static factory and on the interface, but not on the class directly). - Now it would be nice to visibly mark the class non instantiable. -- Andreas