Re: [RFC] Static class
| From: | Lanre | Date: | Fri, 12 Jul 2024 03:13:53 +0000 |
| Subject: | Re: [RFC] Static class | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-124401@lists.php.net to get a copy of this message | ||
On Thu, Jul 11, 2024 at 6:16 PM Bilge <bilge@scriptfusion.com> wrote:
> On 25/06/2024 16:17, Derick Rethans wrote:
>
> we shouldn't be encouraging static classes as a bag of static functions, that ought to be
> just namespaced functions.
>
> cheers,
> Derick
>
> Can someone clue me in as to why grouping related functions in a file with
> nothing but a namespace is strictly better than those same related
> functions grouped as members of a class? It doesn't have to be Larry or
> Derick, because even though they have expressed this view, I am aware they
> are not the only ones whom hold it. Anyone who can shed some light on this
> perspective is welcome to comment.
>
> Cheers,
> Bilge
>
It isn't implicitly better, but if PHP had proper support for autoloading
functions, I would at least consider them equivalent when all the
functions/methods are public.
My primary language is C++, where the distinction is even blurrier since
both static class members and namespace members are accessed with '::'. For
example, X::Y() could be a namespaced function or a static method, and it
doesn't actually matter which it is under the hood since they are
functionally identical.
In PHP, however, putting all your functions in a namespace is less
performant than using static classes. For instance, if you have ten
functions.php files each for a separate namespace, Composer will load all
ten for every single request, regardless of whether they are used.
Additionally, if you install a Composer package that registers some
functions, those will also be loaded for every request.
Cheers,
Lanre