Detail Introduction to PHP Object Oriented Programming with Examples


About Object Oriented Programming

Object oriented programming (OOP) was introduced in PHP 5. This new technique of programming allows us to define classes and create objects. The aim is to reduce rewriting of the same code again and again while increasing reusability of the available code. OOP also makes it simper to edit and update our code.

OOP looks a bit complex at first but once you begin to understand it, you will know how much efficient it is. It is not absolutely necessary to program the OOP way and you can still stick to the procedural method. However, it is considered as a good practice to write code in OOP and there are a lot of benefits. If you thought functions in PHP made it easier, think about how much better it will be to have a collection of functions inside a single class that is reusable. Besides, some languages like Java require you to write code the OOP way. So, you might as well get the hang of it.

In OOP, we define classes and use those classes to create objects. Think of classes as blueprints and objects as the actual buildings that we build. We can build as many buildings or houses we want using the blueprint. Classes in programming are no different. Define a class and use it to create as many objects as you want to.

Basic Terminology Used in Object Oriented Programming

There are a lot of new terminologies used in OOP, but let’s just get accustomed to the basic ones.

Class: A template that contains methods and properties for use in objects. This is the main blueprint.
Objects: When you create new instance of classes, it is known as an object. You can create many objects from a single class.
Methods: In simple words, they are functions that are inside a class.
Properties: Simply saying, they are variables that are present inside classes.

Public vs. Private vs. Protected

While declaring properties and methods inside classes, they can be of different types. They can be public, private or protected. This is known as their visibility.

Here is how they differ:

  1. Public: Can be accessed anywhere from your PHP code. That means, you can get their value or access the methods from other classes and object instances.
  2. Private: Can’t be accessed on outside of class. Their value or method can be used inside that class only. They are exclusive to a single class.
  3. Protected: Can only be accessed from within your class or the classes that are extended to inherit the parent class.

Creating a Class

Here’s an example of how you create a class

Here’s how you create a new object from the class

You can create multiple objects from the same class as:

A class can have different properties.

Example:

It can also have methods.

Example:

Note: See how the properties are accessed using $this->

Accessing Properties and Methods

Objects are created out of classes. These objects utilize the properties and methods present inside those classes.

Here’s how you access properties inside classes

Note: See how -> operator is used to access properties inside classes.

Methods can be created inside classes and they can be used by objects.

Example:

Note: See how -> is used to access methods inside classes.

Creating Multiple Instances of Objects

Classes can have as many objects as you want. Just use a different name for them and you will create multiple instances of a single class. This is how code reusability works.

Example:

__construct Methods

__construct is a special method used in classes. This constructor method is executed immediately when a class is called. It is like the default method. It can also take in parameters or arguments.

Example:

Note: See how the output is generated even though we have not specified a line to access the __construct method.

Inheritance of classes using extends

One class can inherit properties and methods from another class. This is called inheritance. When one class inherits another, the new class will have access to all the public and protected methods and properties of the parent class. The new class can then have additional lines of code.

For example

Note: Even though the property $living is not defined in the class girl, it is inherited from the parent class person.

You can simply override the values of public and protected methods and properties in the parent class by defining the variable again. If the property in the parent method is a boolean, it seems to throw an error when trying to redeclare them.

Example:

Note: To prevent overriding, you can declare them as final. Like: “final public $living” would disable overriding in the above example.

Constants

If you want the value assigned to a property to not change, then we make use of constants. We have to define them and access them in a different way.

Notes:

  • We do not need to create a new object to access the constant change.
  • Using $echo $object->change won’t work even when we create a new object.
  • If we have to access the constant within the class itself we use self::constantname. Using $this-> won’t work.

Static Keyword

Static keywords is used to access properties and methods without creating a new instance of the class. We use the “::” operator just like we use for accessing constants.

Example:

Checking Existence

Simple but important built in methods to check whether objects instances, properties and methods exists or not.

is_a: Checks whether an object is an instance of a class.
property_exists: Checks whether a property exists or not within a new instance of a class.
method_exists: Checks whether or not a method exists within a new instance of a class.

Example:

Naming standards

Here are are the popular naming standards for public, private and protected elements:

  • public $yourName;
  • private $_yourName;
  • protected $_TyourName;

Conclusion

This is a long but a basic introduction to PHP classes. There’s a lot more to discover in PHP OOP. Making the use of this technique in real life PHP scripts for querying databases and creating email forms for example is another challenge. Those who are used to procedural method of programming, might think that using user defined functions can easily do what classed can do. But classes are a lot more than that and we will discover about them soon.

You may also like

Comments are closed here.

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.