Object Oriented Programming Concepts in PHP | Introduction

Object oriented programming concept is a programming concept in which all properties and behavior of entities are grouped into a single entity called a class. This style is considered much more advanced and efficient than the Procedure style of programming. It gives us modularity, reusability and better organization of code.

In this article, we will be discussing some basics on Object Oriented Programming Concepts in PHP. Also, creating a simple PHP class will be discussed.

What is Object Oriented Programming (OOPs)?

Suppose you want to build a car. Every car is expected to have the following systems in it installed.

  • There has to be a body in every car.
  • By default, every car will have wheels, steering and an engine. The necessary things will perform a definite function. For example, a wheel will always rotate when the car is running and the steering will be used to control the direction of a running car.
  • There has to be seats for passengers to sit in every car.

Now, we can have a few features customized in every car depending on its manufacture and price.

  • Every car can be of a different colour.
  • The car might be running on a different fuel (like petrol, diesel, CNG etc).
  • Some cars can have automatic gears while others may be manual shifted.
  • Every engine of a car has a basic function, to make the car run. However, an engine could have a different fuel average in different cars. Also, an engine of car model A might have some differences in its internal processes than a car of model B.

If you were to make a new car, you won’t build each and every feature from scratch. Many times a few features can be reused in another car, and a few could be optimized to meet our requirements.

Modeling Car Example to OOPs

In the scenario described in the above section, we can comprehend that a car is a group and each individual cars are its entities. All the cars belonging to the car ‘group’ should have a few sets of features like (steering, engine, body etc). However, due to the difference in manufacturing, some features may behave differently in different cars.

We now define the car ‘group’ as a Class. And each car is an Object of the class Car.

Class & Objects - Object Oriented Programming Introduction

Now the entities like the steering, wheels, and engine are the Member Variables in the Car. Moreover, the behaviors which can be performed are the Member Functions.

Basic Object Oriented Programming Concepts

For an instance, let’s define some important terms used in Object Oriented Programming Concepts:

Class: Class is the blueprint of your object. A Class is a user-defined data type, which contains variables and functions (called methods). Therefore, you can consider it as a template for creating many instances of the class.

Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as an instance.

Member Variables: They are the variables inside the class. The variables may or may not be visible outside the class. They are the attributes of the object once it is created.

Member Functions: These are the functions defined inside the class to access object data.

Basic Class in PHP

Below is an example of a basic class in PHP.

<?php
	class Car {

		var $name;
                
		function setName($name) {
			$this->name = $name;
		}

		function getName() {
			return $this->name;
		}

	}
?>

A class Car is defined above. Currently, there is only one member variable in the class called name. Also, there are two member functions namely setName and getName. The $this is the reference to the current object.

By default, all members of a class are public. They can be accessed from outside the class. There are other access modifiers also, which will be discussed in a later post.

Let’s instantiate two objects of our class and call the methods. The ‘new’ keyword can be used to instantiate class objects. We can refer to an object’s members by using ‘->’.

<?php
	$bmw = new Car();
	$bmw->setName('BMW Series 3');

	$audi = new Car();
	$audi->setName('Audi A4');

	var_dump($bmw->getName()); //string(12) "BMW Series 3"
	var_dump($audi->getName()); //string(7) "Audi A4"
?>

For example, we have instantiated two objects (bmw and audi) of the Car class. The two objects call the setName method to set a different name respectively. If you print a class object using the print_r function, you will get the following output.

<?php
	print_r($bmw);
	/*
	Car Object
	(
	    [name] => BMW Series 3
	)
	*/
	print_r($audi);
	/*
	Car Object
	(
    	[name] => Audi A4
	)
	*/
?>

Conclusion

In conclusion, we discussed Object Oriented Programming Concepts in PHP. We discussed a basic Car example to model a simple class. Also, we created a simple class in PHP. We will be discussing Classes and Object Oriented Programming in detail in my later posts.

Up Next: Read about Access Modifiers in PHP.

Spread the Knowledge

Leave a Reply

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