1. Code
  2. Coding Fundamentals
  3. OOP

Object-Oriented PHP for Beginners

Scroll to top

For many PHP programmers, object-oriented programming is a frightening concept, full of complicated syntax and other roadblocks. As detailed in my book, Pro PHP and jQuery, you'll learn the concepts behind object-oriented programming (OOP), a style of coding in which related actions are grouped into classes to aid in creating more compact, effective code.

To see what you can do with object-oriented PHP, take a look at the huge range of PHP scripts on CodeCanyon.

Understanding Object-Oriented Programming

Object-oriented programming is a style of coding that allows developers to group similar tasks into classes. This helps keep code following the tenet "don't repeat yourself" (DRY) and makes it easy to maintain.

One of the major benefits of DRY programming is that, if a piece of information changes in your program, usually only one change is required to update the code. One of the biggest nightmares for developers is maintaining code where data is declared over and over again, meaning any changes to the program become an infinitely more frustrating game of Where's Waldo? as they hunt for duplicated data and functionality.

OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually very straightforward and is ultimately a simpler approach to programming.

Understanding Objects and Classes

Before you can get too deep into the finer points of OOP, a basic understanding of the differences between objects and classes is necessary. This section will go over the building blocks of classes, their different capabilities, and some of their uses.

Recognizing the Differences Between Objects and Classes

A House BlueprintA House BlueprintA House Blueprint
Photos from Wick Buildings

Right off the bat, there's confusion in OOP: seasoned developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however, though the difference can be tough to wrap your head around at first.

A class, for example, is like a blueprint for a house. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn't exist.

An object, then, is like the actual house built according to that blueprint. The data stored in the object is like the wood, wires, and concrete that compose the house: without being assembled according to the blueprint, it's just a pile of stuff. However, when it all comes together, it becomes an organized, useful house.

Classes form the structure of data and actions and use that information to build objects. More than one object can be built from the same class at the same time, each one independent of the others. Continuing with our construction analogy, it's similar to the way an entire subdivision can be built from the same blueprint: 150 different houses that all look the same but have different families and decorations inside.

Structuring Classes

The syntax to create a class is pretty straightforward: declare a class using the class keyword, followed by the name of the class and a set of curly braces ({}):

1
<?php
2
3
class MyClass
4
{
5
  // Class properties and methods go here

6
}
7
8
?>

After creating the class, a new class can be instantiated and stored in a variable using the new keyword:

1
$obj = new MyClass;

To see the contents of the class, use var_dump():

1
var_dump($obj);

Try out this process by putting all the preceding code in a new file called test.php in your local testing folder:

1
<?php
2
3
class MyClass
4
{
5
	// Class properties and methods go here

6
}
7
8
$obj = new MyClass;
9
10
var_dump($obj);
11
12
?>

Load the page in your browser at https://localhost/test.php, and the following should display:

1
object(MyClass)#1 (0) { }

In its simplest form, you've just completed your first OOP PHP script.

Defining Class Properties

To add data to a class, properties, or class-specific variables, are used. These work exactly like regular variables, except they're bound to the object and therefore can only be accessed using the object.

To add a property to MyClass, add the following code to your script:

1
<?php
2
3
class MyClass
4
{
5
  public $prop1 = "I'm a class property!";
6
}
7
8
$obj = new MyClass;
9
10
var_dump($obj);
11
12
?>

The keyword public determines the visibility of the property, which you'll learn about a little later in this chapter. Next, the property is named using standard variable syntax, and a value is assigned (though class properties do not need an initial value).

To read this property and output it to the browser, reference the object from which to read and the property to be read:

1
echo $obj->prop1;

Because multiple instances of a class can exist, if the individual object is not referenced, the script would be unable to determine which object to read from. The use of the arrow operator (->) is an OOP construct that accesses the contained properties and methods of a given object.

Modify the script in test.php to read out the property rather than dumping the whole class by modifying the code as shown:

1
<?php
2
3
class MyClass
4
{
5
  public $prop1 = "I'm a class property!";
6
}
7
8
$obj = new MyClass;
9
10
echo $obj->prop1; // Output the property

11
12
?>

Reloading your browser now outputs the following:

1
I'm a class property!

Defining Class Methods

Methods are class-specific functions. Individual actions that an object will be able to perform are defined within the class as methods.

For instance, to create methods that would set and get the value of the class property $age of a Person, add the following to your code:

1
<?php
2
3
class Person
4
{
5
  public $age = 28;
6
7
  public function setAge($newage)
8
  {
9
      $this->age = $newage;
10
  }
11
12
  public function getAge()
13
  {
14
      return $this->age. "<br />";
15
  }
16
}
17
18
$p = new Person;
19
20
echo $p->getAge(); // Get the age value

21
22
?>
OOP allows objects to reference themselves using $this. When working within a method, use $this in the same way you would use the object name outside the class.

To use these methods, call them just like regular functions, but first, reference the object they belong to. Read the property from Person, change its value, and read it out again by adding the lines below:

1
<?php
2
3
$p->setAge(30); // Set a new value for age

4
echo $p->getAge(); // Read it out again to show the change

5
6
?>

Reload your browser, and you'll see the following:

1
28
2
30

"The power of OOP becomes apparent when using multiple instances of the same class."

1
<?php
2
3
// create two objects

4
$p1 = new Person;
5
$p2 = new Person;
6
7
// get the age property for both objects

8
echo $p1->getAge();
9
echo $p2->getAge();
10
11
// set different age for both objects

12
$p1->setAge(18);
13
$p2->setAge(25);
14
15
// output both objects' age values

16
echo $p1->getAge();
17
echo $p2->getAge();
18
19
20
?>

When you load the results in your browser, they read as follows:

1
0
2
0
3
4
18
5
25

As you can see, OOP keeps objects as separate entities, which makes for easy separation of different pieces of code into small, related bundles.

Magic Methods in OOP

To make the use of objects easier, PHP also provides a number of magic methods, or special methods that are called when certain common actions occur within objects. This allows developers to perform a number of useful tasks with relative ease.

Using Constructors and Destructors

When an object is instantiated, it's often desirable to set a few things right off the bat. To handle this, PHP provides the magic method __construct(), which is called automatically whenever a new object is created.

For the purpose of illustrating the concept of constructors, add a constructor to the Person class that will set the $age property and output a message whenever a new instance of the class is created:

1
<?php
2
3
class Person
4
{
5
  public $age;
6
  
7
  public function __construct($age)
8
  {
9
      $this->age = $age;
10
      echo 'The class "', __CLASS__, '" was initiated!<br />';
11
  }
12
13
  public function setAge($newage)
14
  {
15
      $this->age = $newage;
16
  }
17
18
  public function getAge()
19
  {
20
      return $this->age. "<br />";
21
  }
22
}
23
24
// Create a new object

25
$p = new Person(20);
26
27
// Get the value of $age

28
echo $p->getAge();
29
30
echo "End of file."
31
?>
__CLASS__ returns the name of the class in which it is called; this is what is known as a magic constant. There are several available magic constants, which you can read more about in the PHP manual.

When initializing an object from the class, we passed the age argument to the constructor to set the $age property.

Reloading the file in your browser will produce the following result:

1
The class "Person" was initiated!
2
20
3
End of file.

To call a function when the object is destroyed, the __destruct() magic method is available. This is useful for class cleanup (closing a database connection, for instance).

Output a message when the object is destroyed by defining the magic method __destruct() in Person:

1
<?php
2
  
3
class Person
4
{
5
6
  // ...

7
    
8
  public function __destruct()
9
  {
10
      echo 'The class "', __CLASS__, '" was destroyed.<br />';
11
  }
12
13
}
14
15
?>

With a destructor defined, reloading the test file results in the following output:

1
The class "Person" was initiated!
2
20
3
End of file.
4
The class "Person" was destroyed.

"When the end of a file is reached, PHP automatically releases all resources."

To explicitly trigger the destructor, you can destroy the object using the function unset():

1
<?php
2
3
// Create a new object

4
$p = new Person;
5
6
// Get the value of $age

7
echo $p->getAge();
8
9
// Destroy the object

10
unset($obj);
11
12
?>

Now the result changes to the following when loaded in your browser:

1
The class "Person" was initiated!
2
20
3
The class "Person" was destroyed.
4
End of file.

Converting to a String

To avoid an error if a script attempts to output the class Person as a string, another magic method is used called __toString().

Without __toString(), attempting to output the object as a string results in a fatal error. Attempt to use echo to output the object without a magic method in place:

1
<?php
2
3
// Create a new object

4
$p = new Person;
5
6
// Output the object as a string

7
echo $p
8
9
?>

This results in the following:

1
The class "Person" was initiated!
2
3
(!) Fatal error: Uncaught Error: Object of class Person could not be converted to string in C:\wamp64\www\test.php on line 32

To avoid this error, add a __toString() method:

1
<?php
2
3
class Person
4
{
5
  // ...

6
  
7
  public function __toString()
8
  {
9
      echo "Using the toString method: ";
10
      return $this->getAge();
11
  }
12
13
}
14
15
?>

In this case, attempting to convert the object to a string results in a call to the getAge() method. Load the test script in your browser to see the result:

1
The class "Person" was initiated!
2
Using the toString method: 20
3
The class "Person" was destroyed.
4
End of file.
In addition to the magic methods discussed in this section, several others are available. For a complete list of magic methods, see the PHP manual page.

Importing a Class Into Another PHP Program

Often, when we create a class, that class will be in its own separate file. So we're going to move our Person class into its own file named Person.php.

Now, to use that class inside another PHP program, we have to include (or "require") it with the require_once() function.

The following example shows how we require the Person class and use it in test.php.

1
<?php
2
3
require_once("Person.php");
4
5
$p = new Person(20);
6
7
echo $p->getAge();
8
9
?>

Load the page in your browser at http://localhost/test.php, and the following should display:

20

Using Class Inheritance

Classes can inherit the methods and properties of another class using the extends keyword. For instance, to create a second class that extends Person and adds a method, you would add the following to your test file:

1
<?php
2
3
class TallPerson extends Person
4
{
5
  
6
  public function __construct()
7
  {
8
      echo 'The class "', __CLASS__, '" was initiated!<br />';
9
  }
10
  
11
  public function speak()
12
  {
13
      echo "I am not just a Person but a " . __CLASS__ . ".<br />";
14
  }
15
}
16
17
// Create a new object

18
$p = new TallPerson;
19
20
// Use a method on child class

21
echo $p->speak();
22
23
// Access a method on the parent class

24
echo $p->getAge();
25
26
27
?>

Upon reloading the test file in your browser, the following is output:

1
The class "TallPerson" was initiated!
2
I am not just a Person but a TallPerson.
3
20

Overwriting Inherited Properties and Methods

To change the behavior of an existing property or method in the new class, you can simply overwrite it by declaring it again in the new class:

1
<?php
2
3
class TallPerson extends Person
4
{
5
  
6
  public function __construct()
7
  {
8
      echo 'The class "', __CLASS__, '" was initiated!<br />';
9
  }
10
  
11
  public function speak()
12
  {
13
      echo "I am not just a Person but a " . __CLASS__ . ".<br />";
14
  }
15
}
16
17
18
// Create a new object

19
$p = new TallPerson;
20
21
// Use a method on child class

22
echo $p->speak();
23
24
// Use a method from the parent class

25
echo $p->getAge();
26
27
?>

This changes the output in the browser to:

1
The class "TallPerson" was initiated!
2
I am not just a Person but a TallPerson.
3
20
4
The class "Person" was destroyed.

Preserving Original Method Functionality While Overwriting Methods

To add new functionality to an inherited method while keeping the original method intact, use the parent keyword with the scope resolution operator (::):

1
<?php
2
3
class TallPerson extends Person
4
{
5
  
6
  public function __construct()
7
  {
8
      parent::__construct(); // Call the parent class's constructor

9
      echo 'The class "', __CLASS__, '" was initiated!<br />';
10
  }
11
  
12
  public function speak()
13
  {
14
      echo "I am not just a Person but a " . __CLASS__ . ".<br />";
15
  }
16
}
17
18
// Create a new object

19
$p = new TallPerson;
20
21
// Use a method on child class

22
echo $p->speak();
23
24
// Use a method from the parent class

25
echo $p->getAge();
26
27
?>

This outputs the result of both the parent constructor and the new class's constructor:

1
The class "Person" was initiated!
2
The class "TallPerson" was initiated!
3
I am not just a Person but a TallPerson.
4
20
5
The class "Person" was destroyed.

Assigning the Visibility of Properties and Methods

Visibility is a new feature as of PHP 5. For information on OOP compatibility with PHP 4, see the PHP manual page.

For added control over objects, methods and properties are assigned visibility. This controls how and from where properties and methods can be accessed. There are three visibility keywords: public, protected, and private. In addition to its visibility, a method or property can be declared as static, which allows them to be accessed without an instantiation of the class.

Public Properties and Methods

All the methods and properties you've used so far have been public. This means that they can be accessed anywhere, both within the class and externally.

Protected Properties and Methods

When a property or method is declared protected, it can only be accessed within the class itself or in descendant classes (classes that extend the class containing the protected method).

Declare the getAge() method as protected in Person and try to access it directly from outside the class:

1
<?php
2
3
class Person
4
{
5
  // ...

6
7
  protected function getAge()
8
  {
9
      return $this->age. "<br />";
10
  }
11
}
12
13
// Create a new object

14
$p = new Person;
15
16
// Trying to access protected field on parent class

17
echo $p->getAge();
18
19
?>

Upon attempting to run this script, the following error shows up:

1
The class "Person" was initiated!
2
3
( ! ) Fatal error: Uncaught Error: Call to protected method Person::getAge() from context '' in C:\wamp64\www\test.php on line 56

Now, create a new method in TallPerson to call the getAge() method:

1
<?php
2
3
class TallPerson extends Person
4
{
5
6
  // ...

7
  
8
  public function callProtectedAge()
9
  {
10
      return $this->getAge();
11
  }
12
}
13
14
15
// Create a new object

16
$p = new TallPerson;
17
18
// Attempt to access protected field on parent class from method in child class

19
echo $p->callProtectedAge();
20
21
?>

This generates the desired result:

1
The class "Person" was initiated!
2
The class "TallPerson" was initiated!
3
20
4
The class "Person" was destroyed.

Private Properties and Methods

A property or method declared private is accessible only from within the class that defines it. This means that even if a new class extends the class that defines a private property, that property or method will not be available at all within the child class.

To demonstrate this, declare getAge() as private in Person, and attempt to call callProtectedAge() from  TallPerson:

1
<?php
2
3
class Person
4
{
5
    
6
  // ...

7
8
  private function getAge()
9
  {
10
      return $this->age. "<br />";
11
  }
12
}
13
14
class TallPerson extends Person
15
{
16
  
17
  // ...

18
19
  public function callProtectedAge()
20
  {
21
      return $this->getAge();
22
  }
23
}
24
25
// Create a new object

26
$p = new TallPerson;
27
28
// Attempt to access protected field on parent class from method in child class

29
echo $p->callProtectedAge();
30
31
?>

Reload your browser, and the following error appears:

1
The class "Person" was initiated!
2
The class "TallPerson" was initiated!
3
4
(!) Fatal error: Uncaught Error: Call to private method Person::getAge() from context 'TallPerson' in C:\wamp64\www\test.php on line 51

Static Properties and Methods

A method or property declared static can be accessed without first instantiating the class; you simply supply the class name, scope resolution operator, and the property or method name.

"One of the major benefits to using static properties is that they keep their stored values for the duration of the script."

To demonstrate this, make $age a static property and add a static method called plusOne() to Person. Then, set up a do...while loop to output the incremented value of $age as long as the value is less than 10:

1
<?php
2
3
class Person
4
{
5
  
6
  public static $age = 0;
7
  
8
  // ...

9
  
10
  public static function plusOne()
11
  {
12
      return "The Person's age is now " . ++self::$age . ".<br />";
13
  }
14
}
15
16
do
17
{
18
  // Call plusOne without instantiating `Person` class

19
  echo Person::plusOne();
20
} while ( Person::$age < 10 );
21
22
?>

When accessing static properties, the dollar sign ($) comes after the scope resolution operator.

When you load this script in your browser, the following is output:

1
The Person's age is now 1.

2
The Person's age is now 2.
3
The Person's age is now 3.

4
The Person's age is now 4.
5
The Person's age is now 5.

6
The Person's age is now 6.
7
The Person's age is now 7.

8
The Person's age is now 8.
9
The Person's age is now 9.

10
The Person's age is now 10.

Specifying Types on Properties 

When defining a property inside a class, you can also specify what type of data the property should hold. 

 "One of the major benefits of using types on properties is that they type check your class variables and signal you when a wrong data type is used."

To demonstrate this, add int to the $age property to ensure it only receives an integer value. Then attempt to pass the value null to $age:

1
<?php
2
3
class Person
4
{
5
  
6
  public int $age; // age should be an integer

7
8
  // ...

9
  
10
}
11
12
$p = new Person();
13
14
$p->age = null;
15
16
?>

Reloading the file in your browser will produce the following error:

1
The class "Person" was initiated!
2
3
(!)Fatal error: Uncaught TypeError: Typed property Person::$age must be int, null used in C:\wamp64\www\test.php on line 29

Commenting with DocBlocks

While not an official part of OOP, the DocBlock commenting style is a widely accepted method of documenting classes. Aside from providing a standard for developers to use when writing code, it has also been adopted by many of the most popular software development kits (SDKs), such as Eclipse and NetBeans, and will be used to generate code hints.

A DocBlock is defined by using a block comment that starts with an additional asterisk:

1
/**

2
 * This is a very basic DocBlock

3
 */

The real power of DocBlocks comes with the ability to use tags, which start with an at symbol (@) immediately followed by the tag name and the value of the tag. DocBlock tags allow developers to define the authors of a file, the license for a class, the property or method information, and other useful information.

The most common tags used follow:

  • @author: The author of the current element (which might be a class, file, method, or any bit of code) is listed using this tag. Multiple author tags can be used in the same DocBlock if more than one author is credited. The format for the author name is John Doe <john.doe@email.com>.
  • @copyright: This signifies the copyright year and name of the copyright holder for the current element. The format is 2022 Copyright Holder.
  • @license: This links to the license for the current element. The format for the license information is http://www.example.com/path/to/license.txt License Name.
  • @var: This holds the type and description of a variable or class property. The format is type element description.
  • @param: This tag shows the type and description of a function or method parameter. The format is type $element_name element description.
  • @return: The type and description of the return value of a function or method are provided in this tag. The format is type return element description.

A sample class commented with DocBlocks might look like this:

1
<?php
2
3
/**

4
 * A simple class

5
 *

6
 * This is the long description for this class,

7
 * which can span as many lines as needed. It is

8
 * not required, whereas the short description is

9
 * necessary.

10
 *

11
 * It can also span multiple paragraphs if the

12
 * description merits that much verbiage.

13
 *

14
 * @author Jason Lengstorf <jason.lengstorf@ennuidesign.com>

15
 * @copyright 2010 Ennui Design

16
 * @license http://www.php.net/license/3_01.txt PHP License 3.01

17
 */
18
class SimpleClass
19
{
20
  /**

21
   * A public variable

22
   *

23
   * @var string stores data for the class

24
   */
25
  public $foo;
26
27
  /**

28
   * Sets $foo to a new value upon class instantiation

29
   *

30
   * @param string $val a value required for the class

31
   * @return void

32
   */
33
  public function __construct($val)
34
  {
35
      $this->foo = $val;
36
  }
37
38
  /**

39
   * Multiplies two integers

40
   *

41
   * Accepts a pair of integers and returns the

42
   * product of the two.

43
   *

44
   * @param int $bat a number to be multiplied

45
   * @param int $baz a number to be multiplied

46
   * @return int the product of the two parameters

47
   */
48
  public function bar($bat, $baz)
49
  {
50
      return $bat * $baz;
51
  }
52
}
53
54
?>

Once you scan the preceding class, the benefits of DocBlock are apparent: everything is clearly defined so that the next developer can pick up the code and never have to wonder what a snippet of code does or what it should contain.

Comparing Object-Oriented and Procedural Code

There's not really a right and wrong way to write code. That being said, this section outlines a strong argument for adopting an object-oriented approach in software development, especially in large applications.

Reason 1: Ease of Implementation

While it may be daunting at first, OOP actually provides an easier approach to dealing with data. Because an object can store data internally, variables don't need to be passed from function to function to work properly.

Also, because multiple instances of the same class can exist simultaneously, dealing with large data sets is infinitely easier. For instance, imagine you have two people's information being processed in a file. They need names, occupations, and ages.

The Procedural Approach

Here's the procedural approach to our example:

1
<?php
2
3
function changeJob($person, $newjob)
4
{
5
  $person['job'] = $newjob; // Change the person's job

6
  return $person;
7
}
8
9
function happyBirthday($person)
10
{
11
  ++$person['age']; // Add 1 to the person's age

12
  return $person;
13
}
14
15
$person1 = array(
16
  'name' => 'Tom',
17
  'job' => 'Button-Pusher',
18
  'age' => 34
19
);
20
21
$person2 = array(
22
  'name' => 'John',
23
  'job' => 'Lever-Puller',
24
  'age' => 41
25
);
26
27
// Output the starting values for the people

28
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
29
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
30
31
// Tom got a promotion and had a birthday

32
$person1 = changeJob($person1, 'Box-Mover');
33
$person1 = happyBirthday($person1);
34
35
// John just had a birthday

36
$person2 = happyBirthday($person2);
37
38
// Output the new values for the people

39
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
40
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
41
42
?>

When executed, the code outputs the following:

1
Person 1: Array
2
(
3
  [name] => Tom
4
  [job] => Button-Pusher
5
  [age] => 34
6
)
7
Person 2: Array
8
(
9
  [name] => John
10
  [job] => Lever-Puller
11
  [age] => 41
12
)
13
Person 1: Array
14
(
15
  [name] => Tom
16
  [job] => Box-Mover
17
  [age] => 35
18
)
19
Person 2: Array
20
(
21
  [name] => John
22
  [job] => Lever-Puller
23
  [age] => 42
24
)

While this code isn't necessarily bad, there's a lot to keep in mind while coding. The array of the affected person's attributes must be passed and returned from each function call, which leaves some margin for error.

To clean up this example, it would be desirable to leave as few things up to the developer as possible. Only absolutely essential information for the current operation should need to be passed to the functions.

This is where OOP steps in and helps you clean things up.

The OOP Approach

Here's the OOP approach to our example:

1
<?php
2
3
class Person
4
{
5
  private $_name;
6
  private $_job;
7
  private $_age;
8
9
  public function __construct($name, $job, $age)
10
  {
11
      $this->_name = $name;
12
      $this->_job = $job;
13
      $this->_age = $age;
14
  }
15
16
  public function changeJob($newjob)
17
  {
18
      $this->_job = $newjob;
19
  }
20
21
  public function happyBirthday()
22
  {
23
      ++$this->_age;
24
  }
25
}
26
27
// Create two new people

28
$person1 = new Person("Tom", "Button-Pusher", 34);
29
$person2 = new Person("John", "Lever Puller", 41);
30
31
// Output their starting point

32
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
33
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
34
35
// Give Tom a promotion and a birthday

36
$person1->changeJob("Box-Mover");
37
$person1->happyBirthday();
38
39
// John just gets a year older

40
$person2->happyBirthday();
41
42
// Output the ending values

43
echo "<pre>Person 1: ", print_r($person1, TRUE), "</pre>";
44
echo "<pre>Person 2: ", print_r($person2, TRUE), "</pre>";
45
46
?>

This outputs the following in the browser:

1
Person 1: Person Object
2
(
3
  [_name:private] => Tom
4
  [_job:private] => Button-Pusher
5
  [_age:private] => 34
6
)
7
8
Person 2: Person Object
9
(
10
  [_name:private] => John
11
  [_job:private] => Lever Puller
12
  [_age:private] => 41
13
)
14
15
Person 1: Person Object
16
(
17
  [_name:private] => Tom
18
  [_job:private] => Box-Mover
19
  [_age:private] => 35
20
)
21
22
Person 2: Person Object
23
(
24
  [_name:private] => John
25
  [_job:private] => Lever Puller
26
  [_age:private] => 42
27
)

There's a bit more setup involved to make the approach object oriented, but after the class is defined, creating and modifying people is a breeze; a person's information does not need to be passed or returned from methods, and only absolutely essential information is passed to each method.

On a small scale, this difference may not seem like much, but as your applications grow in size, OOP will significantly reduce your workload if implemented properly.

Not everything needs to be object oriented. A quick function that handles something small in one place inside the application does not necessarily need to be wrapped in a class. Use your best judgment when deciding between object-oriented and procedural approaches.

Reason 2: Better Organization

Another benefit of OOP is how well it lends itself to being easily packaged and cataloged. Each class can generally be kept in its own separate file, and if a uniform naming convention is used, accessing the classes is extremely simple.

Assume you've got an application with 150 classes that are called dynamically through a controller file at the root of your application filesystem. All 150 classes follow the naming convention class.classname.inc.php and reside in the inc folder of your application.

The controller can implement PHP's __autoload() function to dynamically pull in only the classes it needs as they are called, rather than including all 150 in the controller file just in case or coming up with some clever way of including the files in your own code:

1
<?php
2
  function __autoload($class_name)
3
  {
4
      include_once 'inc/class.' . $class_name . '.inc.php';
5
  }
6
?>

Having each class in a separate file also makes code more portable and easier to reuse in new applications without a bunch of copying and pasting.

Reason 3: Easier Maintenance

Due to the more compact nature of OOP when done correctly, changes in the code are usually much easier to spot and make than in a long spaghetti code procedural implementation.

If a particular array of information gains a new attribute, a procedural piece of software may require (in a worst-case scenario) that the new attribute be added to each function that uses the array.

An OOP application could potentially be updated as easily as adding the new property and then adding the methods that deal with said property.

A lot of the benefits covered in this section are the product of OOP in combination with DRY programming practices. It is definitely possible to create easy-to-maintain procedural code that doesn't cause nightmares, and it is equally possible to create awful object-oriented code.

Summary

At this point, you should feel comfortable with the object-oriented programming style. Learning OOP is a great way to take your programming to that next level. When implemented properly, OOP will help you produce easy-to-read, easy-to-maintain, portable code that will save you (and the developers who work with you) hours of extra work.

And if you want to purchase affordable, high-quality PHP scripts, you can find thousands of them on CodeCanyon.

This post has been updated with contributions from Kingsley Ubah. Kingsley is passionate about creating content that educates and inspires readers. Hobbies include reading, football and cycling.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.