Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
12bitfloat417819dThe weirdest quirk is really PHP itself
Root5818619dVery little is well supported in PHP. 😋
In most languages constants have to be a value that can be defined at compile time. Instances are not that by most definitions.
Quirinus84419dConstants in PHP are also global, so it's the same shit as doing "global $fakeConst;". So your point about global does not stand. If you dont want to use global, pass the connection. (Class consts are not global though.) Also, using "global" is better than using constants in my opinion, as it doesnt hide the dependency of the method, like using constants does (passing also doesnt hide).
Second, how is passing an connection object to a class constructor not OOP? In actuality, it is a foundation of some of the OOP practices.
Maybe you can keep it as a property in one class that handles all the database stuff, and avoid passing it as an argument, and having DB actions in multiple classes.
C/C++ is an example of literal constants, and many other languages... so it is by no means anything strange. It is actually the norm.
C0D44764318d@Quirinus you said it.
Almost every OOP project I've worked with has a DB class and queries are built in there respective classes then thrown at the db class to parse and do what it has to do to return the result/resultset.
Keeps the "global" issue out of the equation as the DB class is standalone and maintains its own connections.
I have also seen the session handler be utilised for storing the db connection object and that being passed down to the db wrapper to deal with, but that can lead to its own issues.
The constant is suppose to be an immutable compile time value anyway, a db connection can change with the new resource when a reconnect occurs, meaning this is not a compile time event but a runtime event.
PHP is very forgiving, but I'm getting the feeling your trying to abuse the constants to avoid using global variables or dependency injection.
And how can you not use OOP in PHP? This isn't 2003 anymore... granted I have a php5 project around somewhere, but 5.6+, any any new project should be on 7+ anyway, so this gives you namespaces and the likes to go as OOP as you can.
Wack689518dNamespaces are a thing since PHP 5.3, there's dependency injection, there's static variables/functions, there are singletons, and even if you choose to ignore all that, constants can also hold arrays and non primitives are passend by reference,
c3r38r17058318d@Quirinus That's why I make something a constant if it's constant, to facilitate access from places where otherwise I would have to use global.
Also, yeah I forgot the "database connection" is actually a database class, and I don't use many classes but I bet it may be inconvenient for some projects.
Finally (hehe) yeah, I've been since told that that's the expected behaviour of constants, but I had never read it anywhere and no language I know behaves like that except PHP. My bad.
PrivateGER1725418dTo be fair, a lot of languages need global constants to be determined at compile time. That isn't really a PHP quirk.