Is it acceptable to use a mix of object oriented style with procedural style in coding PHP?

StackOverflow https://stackoverflow.com/questions/16756002

  •  30-05-2022
  •  | 
  •  

質問

So before I always used to use procedural style to code php, such as:

mysqli_connect

or

mysqli_prepare

And now I accidentally began to mix it all up and I would do something like:

mysqli_connect();

which is procedural style, and then the next command:

$mysqli->prepare();

which is object oriented style.

My code still works, but if I look at php.net whenever it shows examples the entire example is either object oriented or procedural. So I am asking if its ok for me to use object and procedural php code at the same time. Thank you in advance!

役に立ちましたか?

解決

While how you code is entirely your decision and unique style, I'd say there are a few factors to consider when deciding on procedural, object oriented, or mixed.

Program specifications -

Primarily, if you are on a team, writing the program for someone else, or following your own specifications, consider whether or not the choice has already been made.

Availability -

Let's face it. Sometimes the best libraries are available in either object oriented or procedural, and not both. In such a case, changing from one style would require using a completely different library, or building a class or function library yourself. An available library may save you time, with the only offset cost being a procedural function in a primarily object oriented program, or vice versa.

Familiarity -

Similar to availability, you may be more familiar with a certain class or set of functions. While you may have time to stop and learn a new class module to add to your knowledge, you may be able to save time by using a procedural library that you've already learned and thoroughly tested. So if you are working on a timeline, you may want to go with the more familiar library. However, if you are researching and learning, then you may want to take the time to read documentation, install, and test a new solution.

Data handling and speed -

One more factor to muse about is how are you handling the data. If the data is within the class, then the class will likely have methods to operate on the data. In such a situation, procedural programming would require obtaining the data from the class or object, operating on the data, and then updating the object. A better design would be to include the function in the object in my opinion.

However, if all of your data handling is outside of the class, then using a function may be faster. If you wanted to use a class method, you would have to load the class, and possibly create an object. Even static methods may be slower than a function. So if speed is a consideration, such as in a loop, then consider how many steps your program and PHP has to go through to get to the function, class, or object.

Looking ahead -

If you are wanting to select between procedural or object-oriented programming, then try to predict what will be most useful in the future. I've found object-oriented programming to be very useful for creating reusable code. I've found procedural programming to be very useful for command line code and organizing and using objects. It's likely these will stay the same as computer science evolves, and so work I've done previously is more likely to be useful again.

In contrast, some libraries and programming languages may encourage a style. PHP supports both styles. But if my overall impression is accurate, then PHP has been moving in the direction of object-oriented styles. If selecting between PHP functions and objects, look and see what version of PHP the functions were created for. Also check to see if any of the procedural functions are depreciated or going to become obsolete. If so, use the object-oriented approach, as this will make your program more useful when those procedural functions are no longer supported.

Hope this provides some considerations. Thanks.

他のヒント

Using both will not break your code and technically "ok", but consider future maintenance of the application by you or someone else. It's a lot easier to read the code if it's consistent and clean and you will save a lot of time later by picking and sticking to one now (preferably OOP).

Worse things happen in the world of coding. Some would say that this is just plain wrong. And others would just get paid and be happy that the code works. It all depends on what sort of programmer you want to be.

http://www.codingconfessional.com/

or

http://pragprog.com/the-pragmatic-programmer

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top