PHP Lessons - Lesson 13 - OOP (Object Oriented Programming) Basics
In previous lessons, I’ve already discussed the following PHP data types: boolean, integer, float, string, and arrays. In this lesson, I’ll introduce one more data type available in PHP — the object.
Objects are somewhat similar to arrays — they can contain various data types including numbers, text, arrays, and even other objects.
Like arrays, objects can hold different data types. When these are stored in an object, we call them properties (or fields). Additionally, objects can contain functions within themselves, and these are referred to as methods.
But objects have two major differences from arrays:
1. Objects can contain methods.
Methods are functions that are called in the context of an object. Associating functions with objects helps define the operations the object performs and the actions that can be executed on it.
2. Objects are created from classes.
Before creating an object, we must define a class. A class specifies the properties and methods of the object. This defines the structure and functionality of the object and makes the data easier to understand and manipulate. When creating an object from a class, we can initialize it with data so that newly created objects aren’t empty.
Objects are a very convenient way to store data. In PHP, they help model real-world concepts. For example, consider a database storing information about books. Each book has an author, a number of pages, a publisher, edition number, a review, and other metadata. Grouping this data into a Book class with properties like author, pages, publisher, etc., allows us to treat objects as real-world books. It’s especially useful when storing data in tables — you can name your classes after table names and your properties after column names, simplifying data operations.
Let’s look at how objects differ from arrays through examples.
PHP 5.2 and above:
Let’s start by defining a class for our objects. Note that we use the singular "class", but we can create many objects (instances) from it:
We use the class
keyword to define a class. Now let’s create some objects:
The new
keyword creates new objects of the Book
class. We call these objects instances of the class. So $book1
and $book2
are both instances of Book
. We use print_r()
to inspect their structure.
Now let’s add some properties to our class using the public
keyword (you’ll learn about protected
and private
later):
class Book { public $author; public $numberOfPages; public $year; public $publishingOffice; public $editionNumber; }
We can now assign values to these properties in object instances like so:
$book1 = new Book; $book1->author = 'Abramenko I.A'; $book1->numberOfPages = 192; $book1->year = 2013; $book1->publishingOffice = 'Moscow'; $book1->editionNumber = 1; $book2 = new Book; print ''; print_r($book1); print_r($book2); print '';
You’ll see that $book1
contains data, and $book2
is empty. Let’s add some methods to retrieve these properties:
public function getAuthor() { return $this->author; }
The $this
keyword refers to the current object. It only works inside the class definition. You can now call methods like:
echo $book1->getAuthor();
Now let’s add two more methods to display the book in a table and a div:
public function displayTable() { $content = '
Author | ' . $this->getAuthor() . ' |
You can now render the output like this:
echo $book1->displayTable(); echo $book2->displayUnformatted();
And don’t forget to add a name
property and corresponding methods!
PHP 5.1.3 and below:
In older versions of PHP, use var
to declare class properties:
class MyClass { var $myArray = array(); }
Comparison with Functional Approach
Let’s say we increment every element in an array using a function:
function increment_all_elements($arr) { foreach($arr as $val) { $arr[] = $val + 1; } return $arr; }
Now let’s wrap this logic in an object:
class WorkWithArray { var $myArray = array(); function increment_all_elements($arr) { foreach($arr as $val) { $arr[] = $val + 1; } return $arr; } } $myObject = new WorkWithArray; $myObject->myArray = array(2, 4, 6, 7); $myObject->myArray = $myObject->increment_all_elements($myObject->myArray); print_r($myObject);
This demonstrates how we can encapsulate data and functionality using objects, making our code more organized and closer to real-world modeling.