📜 ⬆️ ⬇️

PHP - OOP or a procedural approach

PHP is one of the most popular scripting programming languages. Almost 60% of web servers use PHP. Millions of websites and web applications are developed in PHP every month.

PHP was originally developed as a simple replacement for the Perl language, and after a couple of years it became extremely powerful and popular. PHP language itself is very similar to ANSI C.
One of the reasons why PHP has become so popular is its short period of study.

Learning PHP is not a hard task at all, especially if you are familiar with the syntax of Java or C.
')
Since writing PHP scripts is quite simple, anyone can write PHP code without complying with any agreements and mixing the presentation layer with business logic (this is one of the main reasons for the existence of a large number of unmanaged projects). Because PHP does not necessarily have strict compliance with the code-writing agreements, as the project gets bigger and bigger over the years, it becomes a huge, unmanaged application.

OOP or Object-Oriented Programming is well used in programming practice for easier creation of managed projects.
A procedural approach involves writing code without using objects. Procedural programming consists of writing code with or without subroutines.

OOP teaches any programming language a better software code and is used to get better performance and write large projects, without fear of becoming entangled in their management. OOP gives you the ability to create objects that can be used repeatedly so that you or other developers can use them in their projects without redoing them again and again. OOP removes barriers and difficulties in writing and managing large applications.

PHP allows us to write applications in 2 different ways, the first is procedural, and the second is object oriented. If you still do not understand the difference between these two approaches, let's look at these pieces of code - the same example written by different approaches.

Procedural:

$ user_input = $ _POST ['field'];
$ filtered_content = filter ($ user_input); // user input filtering
mysql_connect ("dbhost", "dbuser", "dbpassword"); // database
mysql_select_db ("dbname");
$ sql = "some query";
$ result = mysql_query ($ sql);
while ($ data = mysql_fetch_assoc ())
{
process ($ data);
}
process_user_input ($ filtered_content);


And here is the same piece of code using OOP:

$ input_filter = new filter ();
$ input_filter-> filter_user_input (); // filter the user inputs
$ db = new dal ("mysql"); // data access layer
$ db-> connect ($ dbconfig); // we wre using mysql
$ result = $ db-> execute ($ sql);
ReportGenerator :: makereport ($ result); // process data
$ model = new Postmodel ($ filter-> get_filtered_content ());
$ model-> insert ();


If you look closely at these 2 pieces of code, you can see that the code using OOP is more readable and easier to read.

The code with OOP is better organized because it is clear which object is being processed. Large applications written in a procedural approach becomes almost impossible to perceive after the release of several versions. Of course, you can follow the strict rules of writing software code, but they are approved by millions of developers who know that this will not give you ultimately manageability and usability of the project if you do not use OOP in your program.
Almost all large applications are written using Object Oriented
approach.

Based on the above, you can make the advantages of using the PLO:

PLO was created to make life easier for developers. Using OOP, you can break your big problems into small problems that are much easier to solve.
The main requirement of OOP: all you want to do is make objects. Objects are a separate small piece of code that can combine data and properties together. In applications, all objects interact with each other.

OOP can be considered better from different perspectives, especially when development time and subsequent development of an application are important to you.
The main benefits of using OOP can be expressed as:

* Reuse : An object is a logical object that has a set of properties and methods and it can interact with other objects. The object can be completely independent or dependent on other objects. The object is usually created to solve specific problems posed. Therefore, when other developers encounter similar problems, they can connect your class to their project and use it without fear that it will disrupt their development process. This avoids DRY, which stands for Don't Repeat Yourself (do not repeat). In procedural or modular programming, reuse is possible only in combination.

* Refactoring : When you need to use refactoring in a project, OOP provides you with maximum benefits, since all objects are small elements and contain their properties and methods as part of themselves. Therefore, using refactoring is relatively easy.

* Extensibility : If you need to extend the functionality of your project, you can achieve better results with OOP. One of the main OOP functionalities is extensibility. You can use object refactoring to add functionality. By working on this, you can still save
the old object compatibility - therefore you can work fine with the same code. Or you can expand the object and create an absolutely new one that will contain all the necessary properties and methods of the parent object from which the new one originates, and then add new functions to it. This is called “inheritance” and this is a very important feature of OOP.

* Support : object-oriented code is easier to maintain because
it follows very strict code-writing conventions and is written in self-explanatory form.
For example, when a developer complements, recycles code, or debugs it, he can easily find the internal structure of the code and maintain the code from time to time. Moreover, when the OOP development team works in your environment, it may be the best solution since you can distribute your code among the team members after breaking it into small pieces. These small parts can be developed as separate objects, therefore developers can work almost independently of each other. In the end, integrating all the parts into one application will not be difficult.

* Efficiency : The idea of ​​OOP was actually designed to increase efficiency and ease the development process. Several design patterns are designed to create more efficient and good code.
Moreover, in OOP, you can reflect on your decisions in a more convenient form than in a procedural approach. Since you break your problem into several small problems and you find a solution for each of them separately, the big problem is solved by itself.

Author's translation from the book Object Oriented Programming with PHP5

PS my first habratopic, if I like it, I will translate the book further, as for me it is quite interesting and informative

Source: https://habr.com/ru/post/23619/


All Articles