Question

I have been coding some OOP in PHP today and after some time, I figured that you can actually combine the two ways of writing PHP, procedural and OOP. My question is, is it considered bad practise to combine them, should I stick to one of them? Or is it even better coding if you can combine them to make it work perfectly the way you want?

Was it helpful?

Solution

You will always have to have some "procedural code" in PHP. For instance, any plain algorithm you write inside a class method is going to be mostly procedural. Also, PHP has procedural entrance points only. Meaning, in Java for instance you have to create a class even to just get your application started. In PHP you will have to have at least one line of plain procedural code which instantiates an object before "OOP PHP" even begins.

So, yes, in practice you'll always mix the two. Use objects where appropriate and where they simplify code; don't force OOP on everything you possibly can even if it makes no sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top