2 min readObject Oriented Programming (OOP) PHP.

Object Oriented Programming (OOP) PHP is a type of programming that helps to develop large, reusable and complex code for a web application. Object oriented programming has a lot of advantages over its counterpart which is procedural php. With OOP, troubleshooting is easier, code can be reused, easy to use and a whole lot more! […]

Object Oriented Programming (OOP) PHP is a type of programming that helps to develop large, reusable and complex code for a web application.

Object oriented programming has a lot of advantages over its counterpart which is procedural php. With OOP, troubleshooting is easier, code can be reused, easy to use and a whole lot more!

OOP in PHP is about classes, properties, methods and objects. When you get to understand how these work, then you are good to go!

So what we will do is dive straight into OOP, in the process, I will explain how each works.

Classes in OOP

Think of classes like the blueprint or the description of objects. For instance, we have building class which defines the features of every building which are the objects. So your house is the object which is under the building class. I hope you understand this.

In PHP, a class can include variables which are called the properties and also functions which are called the methods. Keep that in mind.

A class definition begins with a class keyword, followed by the name of the class. Most programmers tend to make the first letter capital but a valid class name starts with a letter or underscore followed letters, numbers or underscores.

After typing the class keyword and the name, you then open curly brackets then inside the curly brackets, we have the properties and the methods.

For example:

class Person { // properties and methods }

That’s our class up there called Person which will store our properties and methods.

Properties in OOP

Properties are variables which are found in the classes. But they are not variables but properties! These properties define the features of our object which is the class.

To create a property in PHP, you create it as a normal variable with the dollar ($) sign, followed by the name of the property or variable. You can set the value immediately or set it later. Most developers set it later.

There is one thing also about properties, a keyword is placed before the property, which I will talk about that soon. There are different keywords but in the code below, I used the public keyword.

class Person { public $age; //property }

Methods in OOP

Methods are also placed in the class and describes the behaviour of the object or class. They are also functions in classes but are called methods!

A method is created the same way functions are created but with a keyword in front of it just as in properties. So you bring your keyword first, followed by function, then the name of the function, parenthesis, and finally, curly brackets.

class Person { public $age; //property public function talk() { //method echo “Hey there!”; } }

Note: We will talk about the keywords placed before properties and methods later in the article.

Objects in OOP

We create objects to access properties and methods in a class. After creating an object of a particular class, the properties and methods of that class are stored in the object.

The process of creating an object in object oriented php is called instantiation. To instantiate an object of a class, use the new keyword.

You can access the properties and methods of a class using the arrow construct (->) followed by the property name or the method name with parenthesis.

class Person { public $age; //property public function talk() { //method echo “Hey there!”; } } // Object below $info = new Person(); //object instantiation $info->age = 20; //assignment $info->talk(); //Hey there!


Similar Post

  • The best way to learn coding [7 Proven strategies]

    Are you a university student hoping to become a programmer, an adult hoping to change your career path or a normal individual wanting to learn how to program to make the next Facebook or Google? Learning programming for the first time might seem difficult to almost anyone learning it. Why do you I say that, […]

    READ MORE
  • Object Oriented Programming (OOP) PHP

    Object Oriented Programming (OOP) PHP is a type of programming that helps to develop large, reusable and complex code for a web application. Object oriented programming has a lot of advantages over its counterpart which is procedural php. With OOP, troubleshooting is easier, code can be reused, easy to use and a whole lot more! […]

    READ MORE
  • Google Discover Optimisation – How to rank in Google Discover

    The moment all bloggers have been waiting for is now here! So, what is Google Discover? Google Discover is a new feature introduced by Google on almost all google.com pages that provides feeds in the form of cards to users. It provides feeds from websites just like Google Search but without searching. So how does […]

    READ MORE

Speak Your Mind

Your email address will not be published. Required fields are marked *