DEV Community

oldcastlehq
oldcastlehq

Posted on

How to write modern PHP application without OOP?

Hello!

I would love to hear your input about this topic.

I can find a lot of resources on how to build web apps using OOP "the right way". However, I cannot see the same with procedural style. There are a few notes here and there, but a void on best practices for modern PHP development using procedural programming. Does procedural programming come down only to avoid deep nesting and writing a bunch of functions? What are the best practices for procedural programming?

I'm asking that, because I'm planning to write an e-commerce website (vanilla PHP, no framework) without OOP. I'm not sure if I will use MVC even. I prefer feature folder structure. I'm curious if anyone had tried anything alike this before.

Top comments (7)

Collapse
 
erikaheidi profile image
Erika Heidi

There is a reason why you can only find good resources around using OOP - because that will keep your code cleaner, it will make it easier to refactor and to reuse the code, and it will make the life of other developers who might eventually come to contribute to your code a LOT more easier.

You don't need to use a framework. You can create your thing using vanilla PHP, and that doesn't mean you have to use purely procedural PHP code. You will end up reinventing autoloading and other features that are already there with object-oriented PHP programming.

How do you plan to handle HTML files? Will you mix them into PHP code? How do you plan to integrate your e-commerce with existing APIs such as payment gateways and credit card companies? It can be really hard and frustrating if you don't want to use OOP.

Can you tell the reasoning behind that? If this was meant for CLI applications, I could totally get it. But if you are dealing with the complexity that web applications have, and even more so with an e-commerce website, it's hard to imagine why.

Collapse
 
blackscorp profile image
Vitalij Mik • Edited

Actually i did this: Here is the Sourcecode github.com/BlackScorp/shop/

it is not finished and i am not going to finish the Project but you can add products there, you can purchase them and pay via Paypal for the products. In the Admin interface you will see that someone has purchased a product and you can generate an invoice which is generated with WKHTMLTOPDF binary.

But iam not recommend to use this script or own script for your online shop. the problem is not a technical one. many things in the shop have to defined a specific way by laws. depends on your target where you sell your goods or services you have to follow the laws there. like describing the product or showing the price changes of the last 30 days and so on. all those non technical stuff are implemented in shops like shopware.

So create your own shop ONLY if this will never goes online and you use it just to learn the language.

About Procedural and OOP. OOP is a paradigm, which means way of thinking. Just because youre using Classes does not Mean you automatically use OOP. ALso just because you write functions only, does not Mean youre NOT using OOP ;)

Take a look at following code (sorry for spaces and tabs :D)

<?php

function Car(){
$_kilometers = 0.0;
$_color = null;
return [
'setColor' => function($color) use(&$_color){
$_color = $color;
},
'getColor' => function() use($_color){
return $_color;
}
]
}

$redCar = Car();
$redCar['setColor']('red');

echo "The car color is: ".$redCar['getColor']();
Enter fullscreen mode Exit fullscreen mode

as you can see i just used a function but this function follows the OOP Principles of Encapsulation

Collapse
 
xereu profile image
Darwin Santos

OOP will only over-abstract your code with no added benefit, your code can be perfectly clean with functional or procedural programming. Also don't bother with the MVC, PHP can easily generate whatever you need on the fly, if you need to reuse a form or some other html object, simply create a function, provide it with parameters for flexibility and use this function wherever necessary. I am not sure if you went ahead with the project but if you did, feel free to ask for help should you need it.

Collapse
 
moha1234566044 profile image
Mohamed Hassan

I advise you not to do that. your code is going to be really messy and you are going to suffer from a lot of security issues since your building an e-commerce. The concept of Spaghetti code is big on php. That is why most of the code you get to find is oop-based .if you are looking for training, build something small. Also learn oop as soon as possible believe me you are going to need it

Collapse
 
evgeniir profile image
Evgeniy • Edited

I can find a lot of resources on how to build web apps using OOP "the right way"

Firstly, 95% of them still describe a procedural style, like class with getters and setters + external serivces with procedures.

Second, do you really understand difference between procedural programming an OOP? If this were the case, this question would not arise.

I'm asking that, because I'm planning to write an e-commerce website (vanilla PHP, no framework)

I hope only for the sake of training.

I'm not sure if I will use MVC even.

In fact you won't anyway, because MVC is not about backend-applications, it was invented in 1979 for desktop applications - folk.uio.no/trygver/2007/MVC_Origi... .
The main reason why it is not suitable for the backend - Controlller in MVC does not interact with View, only with models, фnd the views update themselves after the model.

I prefer feature folder structure.

That's good.
I will also mention that MVC(and other patterns or quality frameworks) has nothing to do with the folder structure

I'm curious if anyone had tried anything alike this before.

You should just try.

In addition, if you are really willing to make an effort, I can recommend the book "Clean Architecture" by Robert Martin, it will answer you questions.

Collapse
 
andrejsstep profile image
Andrejs Stepanovs

Sorry its a bit off-topic because I have no answer to what you are asking. But I can strongly advise you not to do it.

E-commerce is hard. If you want to build it from scratch you probably do not grasp the task ahead of you.

OOP will keep your code in some kind of structure. It will allow you to refactor it. It will allow you not to reinvent the wheel over and over and over again by using publicly available libraries (packagist.org).

If you will turn your back to OOP then you will end up with big unmaintainable PHP spagetty code like it was in year 2010.

Collapse
 
deneskellner profile image
Dénes Kellner • Edited

It's absolutely possible, and since OOP serves no real purpose on web, you can produce cleaner, more readable, better performing code if you avoid the bureaucracy forced by Uncle Bob and all the similar books.

Many will be surprised reading this, and their next thought will probably be "this guy doesn't know what he's talking about". Believe me I do. I use objects when I need them; same way as I use any technique when I need it, but code should never be something-ORIENTED. Today OOP is getting less and less attention because many programmers realized it's simply not helping - unfortunately, some of them are moving on to "functional programming" which is not even a thing, and some will simply abandon languages like php because surely, if a paradigm doesn't work, the language is wrong.

No. OOP itself is wrong.

PHP, just as many languages, is perfect if you know what you're doing and why. Any style, any self-developed framework, any folder structure, any business logic is fine as long as you understand your own work. I hope you create something awesome!