php - Object Oriented Naming Conventions


What is the name of having code structured with methods attached to objects?

For example:

" ... ".trim 



At first many would argue this is Object oriented, butphp is Object Oriented(well partially), yet it syntax is completely differenttrim(" ... ").

TL;DR "Object Oriented" and "Everything is an Object" aren't valid answers unlesss......

People constantly suggest PHP is "Object Oriented" and libraries are "Object Oriented", yet PHP library's code is often structured with tons of static classes.

For example RedBean:

  • R::dispense($bean)
  • R::store($bean)
  • R::trash($bean)

That doesn't follow theobj.method syntax, yet is supposedly Object Oriented. Is PHP's object orientation misleading, or is there a better name forobj.method() coding style.




PHP can do both. You can write object oriented code, and you can write procedural code. Procedural code is much easier to understand and write, and most PHP programmers don't really understand OOP, so they write, what I call, procedural code disguised as OOP. This includes things like static methods, and singletons.




Much of the core functions of PHP is C heritage, so don't get surprised if it doesn't follow OOP conventions.

The important thing is that calling methods on objects does follow OOP conventions.




Strings in PHP are not considered to be objects but scalar values. Therefore the syntax"..."->trim() cannot work unless the scalar string literal is converted to object first.

There is an open proposal for auto-boxing (auto-converting of a scalar value to object). This would allow the usual object oriented syntax. Auto-boxing, however does have a little performance penalty. See more here:




In the case of RedBeanPHP I use static methods for the facade. This is more user friendly than purist OO code. You don't have to know about all the internals of RedBeanPHP in order to use it (there is also an OO way to use the lib). In my opinion RedBeanPHP is really object oriented; behind the facade it uses inheritance, interfaces, polymorphism and patterns like adapters, factories, observers.

Also, I believe OOP is more like a way of thinking than just a syntax thing. For instance you can just as well craft object oriented systems using structs and function pointers or like GTK does. OOP features in languages are just to facilitate. Also, because PHP is a dynamic language (in contrast to static typed) it does not have to rely on types, it can just scan objects for desired behaviours. In my opinion this is a good thing because it is more flexible and more maintainable the deep class hierarchies like in Java. I used to be an OO purist but I have recently embraced OOP-pragmatism; just use the best of both worlds.

People are also looking for solutions to the problem: MySQL, php table question (delete on an update)


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.