Introduction To Object-oriented Programming In Php

Introduction To Object-oriented Programming In Php

Learn the programming approach used to build enterprise products written in PHP.

Hello readers and welcome to my blog again. Happy new year to you and cheers to a productive year ahead. Have you been hearing of object-oriented programming in PHP and why you should learn it but are yet to get a grasp the idea? This blog post should walk you through the basics of object-oriented PHP in a very simplified form.

Prerequisite

I assume you have a basic understanding of how variables, loops, functions are created and used in PHP.

Object-oriented Programming

Object-Oriented Programming (OOP) concepts were introduced into the language in Php 5. Up until then, Php was being written in procedural form.OOP is an approach to programming that is based on classes and objects while in procedural/functional programming the focus is on writing procedures/functions to perform a specific task. In OOP, objects created from a class contains properties and methods from the class that perform a specific task.

Features that make object-oriented programming enterprise approach to creating software using PHP.

  1. It helps prevent the repetition of the same piece of code all over the program. Therefore, making code maintenance, modification, and debugging easy.

  2. It provides your program with a clear structure.

  3. It shortens development time, therefore reducing cost. Because time is money.

  4. It makes it easy for multiple persons to collaborate on a single project. And this is one of the most important reasons enterprise projects used object-oriented programming because you will be working with other developers.

Classes and Objects

A class is a collection of properties and methods and is independent of other classes. An object is an instance of a class. A class serves as a template that contains variables and methods to perform a specific or list of tasks. While an object is an individual instance of a class(i.e., an object instance of a class is a copy of that class created using the class as a sample), each object inherits the variables and methods of the class. Enough theory let me show you how a class is created. Let’s name this file "MyClass.php"

// myclass
class MyClass {

}

Now let’s create an object instance from the MyClass class. Please note that there are two ways to do this.

require "MyClass.php"

// first way // best practice
$myfirstobject = new MyClass();

// second way
$mysecondobject = new MyClass;

To create a class you write the class keyword followed by the name you want to name your class and then a curly bracket. To create an instance of a class you assign a variable, you assign the value of that class to a variable after the equal to sign you put the new keyword followed by the class name. The best way is to add () after the class name while you can do without it, I do advise you stick to the first way.

Properties

Properties are similar to variables in that they are both used to assign variables but properties are declared differently. Below is how class properties are created in PHP.

// properties
class MyClass {

         public $myfirstname = 'Yahaya';
         protected $mysecondname = 'Yusuf';
         private $myothername = 'Olansunkanmi';
}

The public, protected, and private keywords are access modifiers. They determine how objects and classes outside of this class interact with these properties. Access modifiers are explained better in a section below.

Methods

Methods are similar to functions in procedural programming. The difference just like in property and variable is the use of access modifiers to determine how objects and classes outside of the class interact with the method in the specific class.

// methods
class MyClass {

          public fucntion setfirstname() {
                     //some code here
}

          protected fucntion setsecondname() {
                     //some code here
}

          private fucntion setothername() {
                     //some code here
}

}

Extending to other classes(Inheritance)

An important feature that OOP provides is class extensibility. We use classes throughout our code. Most time we need to use some properties and methods from another class to get some operations done. This feature is also referred to as inheritance(the class that extends another inherits the properties and methods of the class it extends from). In the example below we create a class named SecondClass and extend the MyClass class created previously. SecondClass now can access the properties and methods from MyClass depending on the access modifiers given to each of them.

When a class extends to another the parent class is also called the base class while the child class can also be referred to as a derived class.

require 'MyClass.php';

// Extendingclass
class SecondClass extends MyClass {

}

Visibility of properties and methods

Visibility is used to determine how methods and properties of a class interact with programs outside of the class. Using the public, protected, or private keyword as a prefix before the property or method. Public visibility is the default visibility; it gives global access to methods and properties. A protected access modifier gives access to methods and properties from within the class and to classes that extends to the class. Private, however, give access only to properties and methods to be used within the class.

Construct and Destruct

When we create an object instance from a class, there is a magical method we can call to perform some tasks immediately. construct is a built-in magic method you can use to carry out some operations you want to occur immediately when the class is created. While destruct on the other hand is called as soon as there are no other references to a particular object in a program e.g closing a database connection.

class MyClass {

         public function __construct() {
                    echo "First to perform <br>";
}

         public function __construct() {
                    echo "Last to perform";
}

}

$newobj = new MyClass();

echo "I will appear in-between construct and destruct <br>";

The above code will give the following output, copy and try it yourself

First to perform

We are in between if construct and destruct are specified

Last to perform

End of output.

Let’s write a small program that sets our first name, last name, and other name and echoes it out, but before we do that let us learn to use the this keyword and arrow (->) symbol. We use the this keyword to refer to the present class. The arrow symbol is used on objects to access the properties and methods of their class.

class MyClass {

         public $firstname;
         public $lastname;
         public $othername;

         public function setfirstname($firstname) {
                    $this->firstname = $firstname;
}

         public function setlastname($lastname) {
                    $this->lastname = $lastname;
}

         public function setothername($othername) {
                    $this->othername = $othername;
}

}

$setname = new MyClass();
echo 'My name are ' . $setname->setfirstname('Yahaya') . ' ' . $setname->setlastname('Yusuf') . ' ' . $setname->setothername('Olasunkanmi');

Summary

Thank you for reading this far, here’s a quick summary of what you should have learned.

  1. You now have a better understanding of Object-oriented programming.

  2. You now know what classes and objects are and how they are created and used.

  3. Properties and methods are similar to variables and functions respectively in how they are been used.

  4. Class properties and methods can be inherited by other classes.

  5. Access modifiers specify how properties and methods are accessed outside of the class.

Thank you for your time and have a wonderful day.

If you still have any questions you can reach me on Twitter or LinkedIn

Credit

Tutorial Republic.

W3School.