Last Updated:

OOP + PHP Programming 

The object-oriented paradigm is a good thing. It so happens that not everyone associates it with scripting languages. This is the first article on OOP+PHP. Introductory, so to speak. The first part.

OOP + PHP

What's the main idea?

In general, object-oriented programming itself is not always simple, but the main idea behind it is quite simple:

The main goal of object-oriented programming is to bind data together (in the form of variables) with the code that works with that data.

Instead of having one large mess of variables and one large mash of code, OOP allows you to break down your problem into smaller (and ideally more organized) bundles of variables and code. These ligaments can work together to achieve the desired result. If you do your job well, then you will have the opportunity to use these bundles in other projects, which will save time and effort.

Here's something we've already realized: OOP alone won't allow you to do something you couldn't do with a mess of code and variables, it's just a more organized method of programming. And if you are somewhat similar to me, then your PHP scripts should use all the help that the department of the organization can provide:

For example, you're tired of coding a database connection every time you develop a new dynamic site based on PHP and MySQL. You can combine all the variables (username, password, hostname, database name, etc.) and all the code (database connection, database selection, error checking) into a single package called an object. You can then use this object to provide interaction with the database in any project.

Don't be square

Actually, the square itself is a trivial thing, but it can be used for a fairly simple starting example.

Let's say some schoolboy who is bad with geometry paid you to write a PHP script to calculate the basic parameters of a rectangle. The market has collapsed, you don't have the opportunity to choose, so you took up this work.

You started with the simplest HTML form so that the student can enter the width and height of the rectangle.

<form action="domyhomework.php" method="get">
Width: <input type="text" name="w" /><br />
Height: <input type="text" name="h" /><br />
<input type="submit" />
</form>

The domyhomework script.php should simply take the height and width and calculate the parameters of the rectangle:

<?php
$area = $w * $h;
$perimeter = ($w + $h) * 2;
?>
<html>
<body>
<p>Width: <?=$w?><br />
Height: <?=$h?></p>
<p>Area: <?=$area?><br />
Perimeter: <?=$perimeter?></p>
</body>
</head>

Simple and beautiful. But if you suddenly gain a reputation as a cool specialist in writing scripts for a rectangle (well, not much ...), then you should soon get tired of writing code to calculate the perimeter and area again and again.

If you're familiar with functions, then you might decide to write two functions for these calculations and put them in a separate file. Let's say you named the file rect.php, and it contains this:

<?php
function rect_area($width,$height)
{
return $width * $height;
}
function rect_perim($width,$height)
{
return ($width + $height) * 2;
}
?>

For now, you can use these features in any file wherever you need them. Then domyhomework.php will change and look like this:

<?php
require('rect.php'); // functions are attached to calculate the parameters of the rectangle
$area = rect_area($w,$h);
$perimeter = rect_perim($w,$h);
?>
<html>
<body>
<p>Width: <?=$w?><br />
Height: <?=$h?></p>
<p>Area: <?=$area?><br />
Perimeter: <?=$perimeter?></p>
</body>
</head>

Things look good right now, but with an object-oriented approach, we can do even better.