On Thu, Nov 23, 2023, at 1:43 AM, Robert Landers wrote:

> I'd venture to say that static classes have some unique benefits over
> plain functions. One, you can encapsulate state using private static
> variables. That simply isn't possible with regular functions (unless
> you use static variables in the functions themselves, which isn't
> quite the same thing). 

It's close enough to the same thing that it has the same problems.

As far as I am aware, a static variable in a function is stored in global 
memory as a singleton.  It's logically the same as a global variable, but with 
access restricted to one function.

A static property of a class is also stored in global memory.  It's logically 
the same as a global variable, but with a funny name that has colons in it.  It 
may be access restricted depending on the code.

(Engine experts, if I am subtly wrong there please do correct me.)

So static variables and static properties are both simply "globals with funny 
extras."  And global-maintained state is well known and understood as a bad 
idea that hinders testing, refactoring, tractability, maintenance, and 
everything else developers care about.

Using global state, with or without funny syntax around it, is the anti-pattern 
we should not be encouraging.

Generally speaking, even my prototypes eschew globals and statics in 95% of 
cases.

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to