What is OOP?

OOP stands for Object Oriented Programming Language.

OOP is a style of coding that allows developers to group a similar task into classes. For example: We can imagine our school is made of different objects like students, teachers, subjects etc. Here, School is a class and the students, teachers, subjects are objects grouped by a class School.

OOP is considered to be more advanced and efficient than the procedural programming style. OOP also help to keep the code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug reducing the development time.

Understandings Class & objects

Before going to the deep, a basic understandings of difference between class and object is necessary.

Recognizing the Differences Between Objects and Classes

Php Classes and Objects
PHP: Class and Object

Developers start talking about class and object and they appear to be interchangeable terms. So, To make the clear understanding of the class and objects I have downloaded the above figures from the internet.

From above pictures I hope that you got the more idea about the difference of class and objects.

A class, for example, Animal. So, the class animal can declare the behaviours and the features of the animals like mammals, birds, fish and reptile.

An object, then, the Animal can have individual objects like cow, lion, dog etc. These are object not only because it has different names, but also because they have different values assigned to their properties. For example in above they can be differ by their family like herbivore or carnival.

How to create classes?

Class is an user defined data type which includes local variables and local methods. In order to define the class in PHP we use the keyword class. Than some basic rules for creating the a class in PHP are as below:

  • The class name should start with the letter and capitalize the first letter
  • The class name should not be a reserved PHP code
  • The class name should not contain any space
  • If the class name contain more than one word we will follow upper camel case method. For example:  WorldTour

For example given below, the class would look like

<?php
    class MyClass
    {
        // Class properties and methods go here
    }
?>

Note: You enclose a class using curly braces { } … just like you do with functions

How to add properties to class?

Properties are declared inside the class. They work exactly like the variable. So it can also accept variable like strings, integers and Boolean (true/false) values. Let’s add some properties to the Animal Class.

class Animal {
  public $family;
  public $food;
}

Here,

  • We have put the public keyword in front of the class property
  • The naming conventions is to start the property name with a lower case letter.
  • If the name contains more than one word, all of the words, except for the first word, start with an uppercase letter. For example: $animalName, $animalColour, $food.
  • A property can be created with or without default value.

How to create Objects from a class?

Several objects can be created from same class, with each objects having its own set of properties. In order to work with class we need to create object. And to create new object we have to create new Keyword. For example:

$cow = new Animal();

Here,

  • We create the object $cow from the class Animal with new Keyword.
  • The process of creating an object is also known as an instantiation.

We can create more than one object from the same class.

$cow = new Animal();
$lion = new Animal();

In fact we can create as many objects as we like from the same class and then give each object its own set of properties.

How to set the Object Properties?

In order to set the food to Grass in the cow object.

$cow->food = ‘Grass’;

Similarly, set the food to Meat in the lion object

$lion->food = ‘Meat’;

And in order to set the value of the $family property for both objects.

$cow->family = ‘Herbivore;
$lion-> family = ‘Carnival;

Once we set the value of the property, we can get its value.

How to get the Object Properties?

Once we create the object and set the value of its properties we can get its value.

In order to get the property value we write the object name, and then dash greater than (->), and then the property name.

echo $cow->food;
echo $lion->food;
echo $cow->family;
echo $lion->family;
RESULT:
Grass
Meat
Herbivore
Carnival


NOTE The property name doesn’t start with the $ sign, only the object name start with the $ sign.

How to add method to a class?

The classes most often contain methods. Methods are class specific function. For example, we will create the method description() to the class.

class Animal {
  public $family;
  public $food;

//Methods
public function description(){
    return ‘Four legged Animals.’;
}

}
  • We have put the public keyword in front of method
  • The naming convention is to start the function name with the lower case letter.
  • If more than one name, than all name except the first should start with the upper case. For example helloUser

We will be able to approach the method in the same way we approach the property. But first we need to create at least one object.

$cow = new Animal();
$lion = new Animal();
echo $cow->description();
echo $lion->description();
RESULT
Four legged Animals.
Four legged Animals.

Here is the full code that we have written during this tutorial.

<?php

//declare class
class Animal {

	//Properties
  public $family;
  public $food;

	//Methods
	public function description(){
	    return 'Four legged Animals.';
	}
}
//create an instance
$cow = new Animal();
$lion = new Animal();

//set the values
$cow->food = 'Grass';
$lion->food = 'Meat';
$cow->family = 'Herbivore';
$lion->family = 'Carnival';

//Get the values
echo $cow->food;
echo "<br />";
echo $lion->food;
echo "<br />";
echo $cow->family;
echo "<br />";
echo $lion->family;
echo "<hr />";
//user the methods to get the four legged animals
echo $cow->description();
echo "<br />";
echo $lion->description();
?>

Conclusion

In this tutorial, we have entered in the first steps into the world of object oriented programming by learning about classes and about the objects that can be created out of them. In the next tutorial, you will learn about the $this keyword that enables us to approach properties and methods within the scope of the class.

If you have any questions, feel free to ask in the comments below .

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

Comments

Leave a Reply

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