Is it some bizarre heresy to use classes without objects?
This may be a silly question, but it's been bugging me.
I've been writing what I believe to be procedural code, but I'm using classes which group together related public and private functions according to purpose. Instead of using objects and methods, I call the functions wh开发者_Python百科en needed with a scope resolution operator. ie: db::execute($sql)
I know it's ridiculous, but I just now realized everyone immediately associates classes with OOP. Am I committing some perverted heresy?
you're basically abusing one language construct (class) to emulate another one (namespace). This is totally normal, as long as you use a language that doesn't support the latter (php 5.2-).
If you know the distinction between classes and "OOP use of classes" then I guess it is not a real problem ...
"Perverted heresy" might be taking it a little far, but you're certainly missing out on much of the organizational power of object oriented programming.
It sounds to me like you're using classes as namespaces.
I don't see anything particularly wrong with this - it provides logical separation of concerns, and presumably makes things easier for you to develop/maintain.
You'd definitely be better off learning more about Object-Oriented Programming so you can take advantage of the benefits it offers.
As long as you don't claim to be doing OOP, I don't see anything wrong with using static methods for procedural programming to make up for PHP's lack of namespaces.
You are not doing anything wrong.
Some classes are used only to contain utility functions (for example the Arrays
class in Java), actually called "static methods".
What you are doing is using this feature in substitution of namespaces. For clarity you should use the latter.
精彩评论