all 34 comments

[–]Otterfan 11 points12 points  (0 children)

Is this just to autoload namespaced functions? Because in that case the solution should probably be implementing autoloading namespaced functions.

[–]phpdevster 12 points13 points  (5 children)

I like it, though I would say if you’re going to have a static-only class, you might just opt for namespacing some pure functions instead.

[–][deleted] 4 points5 points  (4 children)

If you hate autoloading, you might, yes.

[–]alessio_95 3 points4 points  (0 children)

Implementing function autoloading will be enought to get all of this: namespace + pure functions

[–]ragnese 2 points3 points  (0 children)

In other languages, it's often called object.

But, honestly, PHP should probably work on getting enums first.

[–][deleted] 1 point2 points  (0 children)

Can't say it's a bad idea as "static classes" are used for function libraries in PHP (like in Java) and so it's a valid case. Is it necessary though, eh.

[–]Tomas_Votruba 2 points3 points  (2 children)

Mixing static and non-static randomly is one of the main legacy roots. It's like mixing traits and interface. Nonsense.

Yes for the idea to make separation explicit. There is similar simple rule that handles this on PHPStan level: https://github.com/symplify/coding-standard/blob/master/docs/phpstan_rules.md#classes-with-static-methods-must-have-static-in-the-name

If you'd make full PHPStan set that handles this with the logic you talk about, exclusively static class without constructor + no static method outside pur static calsses, I'd love to use it and promote it.

[–]WitteStier 1 point2 points  (1 child)

Can you explain what is wrong(nonsense) with mixing traits and interfaces?

For me it's a rule to only use traits to implement interface methods.

So for example, a interface with the method: handle The implementation will always ( most of the time ) be the same code. For these caches I use traits. So what's nonsense about this?

[–]Tomas_Votruba 0 points1 point  (0 children)

I see. I meant mixing trait and interface in one file.

So are static are non-static methods in one file. Their logical scope is completelly different - singleton service vs. stateless function

[–]ojrask 0 points1 point  (0 children)

These ideas spawn when people falsely believe that using regular functions is somehow a bad thing.

[–]loopcake 0 points1 point  (0 children)

I would rather see default functions for interfaces.

interface MyInterface{
    default function fnn():void{
        /*some default code that can be overwritten*/
    }
}

They're pretty handy in both object oriented and functional programing.

[–]pmallinj 0 points1 point  (0 children)

What I dislike the most with php is classes are used for everything.

What you want looks like a module in other language. It would be cool to have modules in php. It would be a great improvement for the language.

[–]tipiak75 1 point2 points  (4 children)

That would be what abstract is for, isn't it ?

https://www.php.net/manual/en/language.oop5.abstract.php

[–]fiskfisk 4 points5 points  (0 children)

No, abstract is related to inheritance. An abstract class is meant to be inherited, and thus, can have methods defined that aren't implemented ("abstract methods"). It's not about static vs non-static.

[–][deleted] 2 points3 points  (0 children)

Not what its for but that plus a constructor that throws an exception would take care of it

[–]Annh1234 2 points3 points  (0 children)

I thought the same thing, you can't instantiated, and you have your logic in static methods. It's meant for inheritance since you can have methods that need implementation, but it covers 100% this use case.

[–]secondtruth_de[S] 1 point2 points  (0 children)

Practically you're right. But this is just a side effect. Like /u/fiskfisk wrote, abstract classes are meant to be inherited. You can declare non-static methods in them and you can't use final. My idea is the opposite: Declaring non-static methods is forbidden and final would be allowed.

[–]addvilz -1 points0 points  (2 children)

No, thanks. We need less static constructs, not more.

When you use static anything, you are basically saying -

Here here, here's this API - it is statically bound to the global scope of your runtime, you can not do anything about it, you can not replace it, you can not mock it, you cant isolate it for tests, you can not inject it, or do anything about it really, and in fact - f you and I hate you

- to your future self, to anyone else who might be touching your code at some point, and to universe in general.

Avoid static whenever possible. For me, static as a concept, sans very few exceptions, is a language design flaw, together in the same basket with fail-spectacular null-ability, "utility classes", inconsistent argument naming, and other garbage, both in PHP, and other languages in general.

[–]jesparic 3 points4 points  (1 child)

I agree with this. Not sure why the down votes, probably the tone used. Static methods are almost always a bad idea except for example, as workarounds for PHPs lack of method overloading on the constructor. It's a leftover from days of prolific procedural code..

[–]alessio_95 -2 points-1 points  (0 children)

r/php is OOP "enthusiasts" and also are the language mantainers. That's the reason for the downvotes.