Object Oriented PHP with classes and object
Unlimited Plugins, WordPress themes, videos & courses! From $16.50/m
Subscribe
- Sign In
Advertisement
Object-Oriented PHP With Classes and Objects
by Sajal Soni4 Dec 2018
Difficulty:BeginnerLength:LongLanguages:Englishالعربية/عربيDeutschEspañolहिंदीBahasa IndonesiaItalianoPусскийTiếng Việt
In this article, we're going to explore the basics of object-oriented programming in PHP. We'll start with an introduction to classes and objects, and we'll discuss a couple of advanced concepts like inheritance and polymorphism in the latter half of this article.
What Is Object-Oriented Programming (OOP)?
Object-oriented programming, commonly referred to as OOP, is an approach which helps you to develop complex applications in a way that's easily maintainable and scalable over the long term. In the world of OOP, real-world entities such as Person, Car, or Animal are treated as objects. In object-oriented programming, you interact with your application by using objects. This contrasts with procedural programming, where you primarily interact with functions and global variables.
In OOP, there's a concept of "class", which is used to model or map a real-world entity to a template of data (properties) and functionality (methods). An "object" is an instance of a class, and you can create multiple instances of the same class. For example, there is a single Person class, but many person objects can be instances of this class—dan, zainab, hector, etc.
The class defines properties. For example, for the Person class, we might have name, age, and phoneNumber. Then each person object will have its own values for those properties.
You can also define methods in the class that allow you to manipulate the values of object properties and perform operations on objects. As an example, you could define a save method which saves the object information to a database.
What Is a PHP Class?
A class is a template which represents a real-world entity, and it defines properties and methods of the entity. In this section, we’ll discuss the basic anatomy of a typical PHP class.
The best way to understand new concepts is with an example. So let's have a look at the Employee class in the following snippet, which represents the employee entity.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
class Employee
{
private $first_name;
private $last_name;
private $age;
public function __construct($first_name, $last_name, $age)
{
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->age = $age;
}
public function getFirstName()
{
return $this->first_name;
}
public function getLastName()
{
return $this->last_name;
}
public function getAge()
{
return $this->age;
}
}
?>
The class Employee statement in the first line defines the Employee class. Then, we go on to declare the properties, the constructor, and the other class methods.
2 Million+ WordPress Themes & Plugins, Web & Email Templates, UI Kits and More
Download thousands of WordPress themes and plugins, web templates, UI elements, and much more with an Envato Elements membership. Get unlimited access to a growing library to millions of creative and code assets.
Android App Templates
Kick-start your next Android app with 5k+ versatile templates.
UX & UI Kits
Easily customizable UX and UI kits to inspire your next project.
PHP Scripts
Over 4,000 PHP scripts, plugins, and add-ons.
Class Properties in PHP
You could think of class properties as variables that are used to hold information about the object. In the above example, we’ve defined three properties—first_name, last_name, and age. In most cases, class properties are accessed via instantiated objects.
These properties are private, which means they can only be accessed from within the class. This is the safest access level for properties. We’ll discuss the different access levels for class properties and methods later in this article.
Constructors for PHP Classes
A constructor is a special class method which is called automatically when you instantiate an object. We’ll see how to instantiate objects in the next couple of sections, but for now you just have to know that a constructor is used to initialize object properties when the object is being created.
You can define a constructor by defining the __construct method.
Advertisement
Methods for PHP Classes
We can think of class methods as functions that perform specific actions associated with objects. In most cases, they are used to access and manipulate object properties and perform related operations.
In the above example, we’ve defined the getLastName method, which returns the last name associated with the object.
So that’s a brief introduction to the class structure in PHP. In the next section, we’ll see how to instantiate objects of the Employee class.
What Is an Object in PHP?
In the previous section, we discussed the basic structure of a class in PHP. Now, when you want to use a class, you need to instantiate it, and the end result is an object. So we could think of a class as a blueprint, and an object is an actual thing that you can work with.
In the context of the Employee class which we've just created in the previous section, let's see how to instantiate an object of that class.
1
2
3
4
5
6
7
<?php
$objEmployee = new Employee('Bob', 'Smith', 30);
echo $objEmployee->getFirstName(); // print 'Bob'
echo $objEmployee->getLastName(); // prints 'Smith'
echo $objEmployee->getAge(); // prints '30'
?>
You need to use the new keyword when you want to instantiate an object of any class along with its class name, and you'll get back a new object instance of that class.
If a class has defined the __construct method and it requires arguments, you need to pass those arguments when you instantiate an object. In our case, the Employee class constructor requires three arguments, and thus we've passed these when we created the $objEmployee object. As we discussed earlier, the __construct method is called automatically when the object is instantiated.
Next, we've called class methods on the $objEmployee object to print the information which was initialized during object creation. Of course, you can create multiple objects of the same class, as shown in the following snippet.
01
02
03
04
05
06
07
08
09
10
11
12
13
<?php
$objEmployeeOne = new Employee('Bob', 'Smith', 30);
echo $objEmployeeOne->getFirstName(); // prints 'Bob'
echo $objEmployeeOne->getLastName(); // prints 'Smith'
echo $objEmployeeOne->getAge(); // prints '30'
$objEmployeeTwo = new Employee('John', 'Smith', 34);
echo $objEmployeeTwo->getFirstName(); // prints 'John'
echo $objEmployeeTwo->getLastName(); // prints 'Smith'
echo $objEmployeeTwo->getAge(); // prints '34'
?>
The following image is a graphical representation of the Employee class and some of its instances.
Simply put, a class is a blueprint which you can use to create structured objects.
Encapsulation
In the previous section, we discussed how to instantiate objects of the Employee class. It's interesting to note that the $objEmployee object itself wraps together properties and methods of the class. In other words, it hides those details from the rest of the program. In the world of OOP, this is called data encapsulation.
Encapsulation is an important aspect of OOP that allows you to restrict access to certain properties or methods of the object. And that brings us to another topic for discussion: access levels.
Advertisement
Access Levels
When you define a property or a method in a class, you can declare it to have one of these three access levels—public, private, or protected.
Public Access
When you declare a property or a method as public, it can be accessed from anywhere outside the class. The value of a public property can be modified from anywhere in your code.
Let's look at an example to understand the public access level.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<?php
class Person
{
public $name;
public function getName()
{
return $this->name;
}
}
$person = new Person();
$person->name = 'Bob Smith';
echo $person->getName(); // prints 'Bob Smith'
?>
As you can see in the above example, we've declared the name property to be public. Hence, you can set it from anywhere outside the class, as we've done here.
Private Access
When you declare a property or a method as private, it can only be accessed from within the class. This means that you need to define getter and setter methods to get and set the value of that property.
Again, let's revise the previous example to understand the private access level.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<?php
class Person
{
private $name;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
$person = new Person();
$person->name = 'Bob Smith'; // Throws an error
$person->setName('Bob Smith');
echo $person->getName(); // prints 'Bob Smith'
?>
If you try accessing a private property from outside the class, it'll throw the fatal error Cannot access private property Person::$name. Thus, you need to set the value of the private property using the setter method, as we did using the setName method.
There are good reasons why you might want to make a property private. For example, perhaps some action should be taken (updating a database, say, or re-rendering a template) if that property changes. In that case, you can define a setter method and handle any special logic when the property is changed.
Protected Access
Finally, when you declare a property or a method as protected, it can be accessed by the same class that has defined it and classes that inherit the class in question. We'll discuss inheritance in the very next section, so we'll get back to the protected access level a bit later.
Inheritance
Inheritance is an important aspect of the object-oriented programming paradigm which allows you to inherit properties and methods of other classes by extending them. The class which is being inherited is called the parent class, and the class which inherits the other class is called the child class. When you instantiate an object of the child class, it inherits the properties and methods of the parent class as well.
Let's have a look at the following screenshot to understand the concept of inheritance.
In the above example, the Person class is the parent class, and the Employee class extends or inherits the Person class and so is called a child class.
Let's try to go through a real-world example to understand how it works.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class Person
{
protected $name;
protected $age;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
private function callToPrivateNameAndAge()
{
return "{$this->name} is {$this->age} years old.";
}
protected function callToProtectedNameAndAge()
{
return "{$this->name} is {$this->age} years old.";
}
}
class Employee extends Person
{
private $designation;
private $salary;
public function getAge()
{
return $this->age;
}
public function setAge($age)
{
$this->age = $age;
}
public function getDesignation()
{
return $this->designation;
}
public function setDesignation($designation)
{
$this->designation = $designation;
}
public function getSalary()
{
return $this->salary;
}
public function setSalary($salary)
{
$this->salary = $salary;
}
public function getNameAndAge()
{
return $this->callToProtectedNameAndAge();
}
}
$employee = new Employee();
$employee->setName('Bob Smith');
$employee->setAge(30);
$employee->setDesignation('Software Engineer');
$employee->setSalary('30K');
echo $employee->getName(); // prints 'Bob Smith'
echo $employee->getAge(); // prints '30'
echo $employee->getDesignation(); // prints 'Software Engineer'
echo $employee->getSalary(); // prints '30K'
echo $employee->getNameAndAge(); // prints 'Bob Smith is 30 years old.'
echo $employee->callToPrivateNameAndAge(); // produces 'Fatal Error'
?>
The important thing to note here is that the Employee class has used the extends keyword to inherit the Person class. Now, the Employee class can access all properties and methods of the Person class that are declared as public or protected. (It can't access members that are declared as private.)
In the above example, the $employee object can access getName and setName methods that are defined in the Person class since they are declared as public.
Next, we've accessed the callToProtectedNameAndAge method using the getNameAndAge method defined in the Employee class, since it's declared as protected. Finally, the $employee object can't access the callToPrivateNameAndAge method of the Person class since it's declared as private.
On the other hand, you can use the $employee object to set the age property of the Person class, as we did in the setAge method which is defined in the Employee class, since the age property is declared as protected.
So that was a brief introduction to inheritance. It helps you to reduce code duplication, and thus encourages code reusability.
Polymorphism
Polymorphism is another important concept in the world of object-oriented programming which refers to the ability to process objects differently based on their data types.
For example, in the context of inheritance, if the child class wants to change the behavior of the parent class method, it can override that method. This is called method overriding. Let's quickly go through a real-world example to understand the concept of method overriding.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
class Message
{
public function formatMessage($message)
{
return printf("<i>%s</i>", $message);
}
}
class BoldMessage extends Message
{
public function formatMessage($message)
{
return printf("<b>%s</b>", $message);
}
}
$message = new Message();
$message->formatMessage('Hello World'); // prints '<i>Hello World</i>'
$message = new BoldMessage();
$message->formatMessage('Hello World'); // prints '<b>Hello World</b>'
?>
As you can see, we've changed the behavior of the formatMessage method by overriding it in the BoldMessage class. The important thing is that a message is formatted differently based on the object type, whether it's an instance of the parent class or the child class.
(Some object-oriented languages also have a kind of method overloading that lets you define multiple class methods with the same name but a different number of arguments. This isn't directly supported in PHP, but there are a couple of workarounds to achieve similar functionality.)
Conclusion
Object-oriented programming is a vast subject, and we've only scratched the surface of its complexity. I do hope that this tutorial helped you get you started with the basics of OOP and that it motivates you to go on and learn more advanced OOP topics.
Object-oriented programming is an important aspect in application development, irrespective of the technology you're working with. Today, in the context of PHP, we discussed a couple of basic concepts of OOP, and we also took the opportunity to introduce a few real-world examples.
Feel free to post your queries using the feed below!
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.
Advertisement
Translations
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!
Powered by
LOOKING FOR SOMETHING TO HELP KICK START YOUR NEXT PROJECT?
Envato Market has a range of items for sale to help get you started.
Unlimited Downloads
From $16.50/month
Get access to over one million creative assets on Envato Elements.
Everything you need for your next creative project.
Create Beautiful Logos, Designs
& Mockups in Seconds
Design like a professional without Photoshop.
Share ideas. Host meetups. Lead discussions. Collaborate.
QUICK LINKSExplore popular categories
ENVATO TUTS+
JOIN OUR COMMUNITY
HELP
29,965
Tutorials
1,308
Courses
48,654
Translations
© 2021 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.
- Follow Envato Tuts+
Advertisement
Advertisement