简体   繁体   中英

Object Oriented Naming Conventions

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

For example:

" ... ".trim 

or

obj.method()

At first many would argue this is Object oriented , but php is Object Oriented(well partially), yet it syntax is completely different trim("... ") .

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 the obj.method syntax, yet is supposedly Object Oriented. Is PHP's object orientation misleading, or is there a better name for obj.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:

https://wiki.php.net/rfc/autoboxing

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM