There is a problem with static variables and functions in how it is used by apps and frameworks in PHP , they are used as global functions or parameters while this is by far what they mean. It is easy to write the same functional or even procedural code in PHP using static variables dressed up as OOP. I have nothing against functional programming or even procedural (my work for some years was turning Cobol into Java , and I have seen real diamonds in the way they done it (along of course of rubbish) ) . There is no reason to pretend OOP and use static methods or variables , just use the old way functional or procedural programming , it is clearer.

What static means is that is goes along with the class itself not the instances of that class. In that sense static properties are application scope caching. While most OOP languages have static initializers you can achieve the same thing in PHP with spl autoload. Of course there is no reason to have Form or Input or anything from a specific request as static. This is just functional code in OOP static properties and methods dressed up.

The main problem in PHP is that we don't speak the same language. PHP is multiparadigm , in fact most write PHP apps in many paradigms in the same context. Everyone have its own mix , and I can't find employees understanding what OOP is in PHP. We are not talking the same language. Their impressions came out of something that is a mix , and they haven't tested their opinions. “Everyone in PHP community is saying the X argument” . Have you tested it ?

Recommended Answers

All 10 Replies

Member Avatar for diafol

I understand what you're saying, but I'm not sure what you're trying to suggest. Yep, frameworks like Laravel (which I use a lot) uses statics where you would normally not. But for rapid development, I simply "don't care" - that is not a flippant nor an 'opinion-tested' statement, just the fact that the time saved allows me to tackle other areas. Were I to have a few months to build my own 'framework', I doubt very much whether it would contain as many (if any) statics. However, my framework would be guaranteed to be leaky, unsecure, bug-ridden and run slowly. Fast forward two years later, my framework is a little bit more secure, contains fewer bugs, still a bit slow, but contains quite a lot of deprecated code. Of course, this depends on what exactly you're leaving up to the framework.

So are you suggesting we dump the big framworks? Should we opt to find a more 'true' OOP framework? Should we be creating our own? I'm not ashamed to say that I still struggle with certain aspects of OOP - I probably always will.

Do you have some code examples that you'd like to share (e.g. proc/func vs static)?

Diafol you put many issues at one question. As it sounds you agree with me that using static properties or methods where have no meaning just is bad OOP programming. I am not against statics at all . Statics have meaning in an application scope wide.

Your intuition is right (in my opinion) ... there is something wrong with those frameworks, but again we should define what a framework is. For some PHP programmers a framework is a program that they run through CLI and convert their code to an app. That for me is a code generator , and not a framework.

For some others they don't even write SQL statements , they don't have a model do their job , everything is done in the Controller side. So yes there is something terrible wrong with the thing that most PHP community embraced OOP.

YES ... we are writing code in an new framework called _Underscore for a couple of years now , BUT even when we release it in public we would not think that we are the 100% correct. To have such certainty indicates that is something really wrong about the core.

There are still PHP frameworks out there that embraces OOP , in a certain way . It is better to do it in CI an know that you are using old tech than mocking your self with statics Input:: and so on. Even Zend Framewrok is better that way

What is the best framework to use? .. I have no idea ... it all has to do with what is you programming background and what programmer you want to be. It is rather silly to try to look OOP if you are not ... don't

Member Avatar for diafol

I do agree with your statements on the indiscriminate use of statics in "frameworks" (or libraries 0 whatever you want to call them) such as Laravel.
I'm not against statics in principle either - they have their place as you say, although they tend to be over-used or used inappropriately.

I've used a number of different PHP "frameworks" over the years. I learned a lot from CI, but the flexibility (to not use a model at all), made me a bit uncomfortable as projects could end up controller-heavy or VC instead of MVC. Some of the others - Symphony - left me cold, Zend - well, didn't want to run their server, Yii - loved it, but a little limited in certain aspects.

Laravel - and I'm no loyal fanboy - I will change to whatever allows me to get the job done to a good-ish standard and give me time to spend with my family - seems to have got me to that point.

_Underscore sounds interesting, do you have a link?

difaol not yet.. the main thing is that English is not my native language , we have wrote a manual in Greek but for that to be translated in English and to be explained as well is a long run to go. Again I am not claiming that _Underscore have it all right but of course is a bell if you use so many statics ... _Undersore lies under JSF meaning that in your app has two objects Url or Action and two options in each controller doGet and doPost ... yes there are statics ... but statics are app scope caching ... (if you know C++ or Java that it is static is all about in an app). _Underscore is just our framework .... I can't except anything matching but if you see many statics wondering around with no reason than you should thing twice (why do they do it that way?) .

doGet and doPost are not static ... are methods of the Controller chosen ... for example http://myapp.com/Helllo means Controller_Hello or http://myapp.com/Helllo/Damta means Hello_Datmta class that is in Hello/Damta.php file . If http://myapp.com/Helllo/Derta there is no Derta but there is a hello Controller than it will go in Hello Controller with doGet and variable Derrta

In any framework in PHP you should ask if it is real OOP or just (microsoft like OOP with statics) or even statics in its core

Member Avatar for diafol

From what i gather laravel does not rely on statics solely as most class statics are actually aliases but ultimately i think they extend a facade class that has statics.

diafol the framework you mention is just a cherry in the ice cream ;) (there are more out there) What motivated me to write this comment is that last weeks I read more threads here of people dealing with static methods and properties in PHP as it were functional programming (The framework you mentioned is not different in that way it could be an input function as well instead of Input::: ) . Every usage of each language has its own reason so I can't say that they are 100% wrong , the tool is there , they are just using it.

My point is that really is NO reason at all to dress functional or even procedural code to OOP , functional and procedural code are OK if you know what you are doing. I am not writing any more functional or procedural but I respect those paradigms. If you see the posts from last days you will realize that is not only that framework you mentioned but almost anybody tries to write functional programming dressed as OOP using static methods and properties.

They can do it , but I believe that would be great to stop a moment and thing of what is the reason to go that way other than “looking like OOP” ?

(diafol because obviously you are a framework creator as well , take a moment and think what kind of abstraction is if you don't have globals served to the Controllers (like $_POST or Input:: ) but objects like Action (from Post) or Url (from Get) . That would allow you to instantiate a new Controller even inside other one with different parameters and get different results, more over would allow you to run the same app from CLI without referring to external programm to deal with your app)

Personally I dislike statics. Apart from that I do not see the use of them in PHP. Every script runs, reinitializing everything, therefor a static offers nothing more than a regular property already does. In other languages an object or static class has a much longer lifetime, so there are situations where they have their use. In PHP statics are just abused because some people think they need globals.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.