Object Oriented Concepts (Oops)

[vc_row][vc_column][vc_column_text]Object Oriented Programming
Oops is an important part of any programming language. Its play a big role in the programming language like c, c++, java, PHP so on.

Object-Oriented Programming Oops is a programming langauge which is based on objects not only procedures and functions

Object Oriented Concepts

Class : Class is consist of methods and properties. A class is defined by using the class keyword, with the name of the class and a pair of curly braces {}. All its methods and properties goes inside the braces: Below is a class. In this simple class. $model is a property and hornBell() is a method. They are all prefixed with a visibility keyword (public).

 

 

<?php

Class Car {

	public $model = “123456”;

	public function hornBell() {
		echo “Ding Dang Dong”;
	}
}
?>

Objects : Objects are also known as instance. You need to define a class one time and then create maltiple objects that belong to it. Objects of a class is created using the new keyword. To instantiate an object of a class, use the keyword new as below:

<?php
$car = new Car();
?>

Objects : Objects are also known as instance. You need to define a class one time and then create maltiple objects that belong to it. Objects of a class is created using the new keyword. To instantiate an object of a class, use the keyword new as below:

<?php

$car = new Car();

?>

Member Variable : The Member variables defined inside a class. This data will not be visible to outside the class and can be accessed by member functions. These variables are called attribute of the object once an object is created.

Member function : The Member function defined inside a class and are used to access object data.

Inheritance : Inheritance is the mechanism of code reuseability. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.

<?php

class Vehicle {
	public function name() {
		echo "I am a Vehicle";
	}
}

class Car extends Vehicle {

}

$car = new Car();
$car->name(); // I am a Vehicle
?>

Parent class : A class that is inherited from by another class. This is also called a base class or super class.

Child Class : A class that inherits from another class. This is also called a subclass or derived class.

Polymorphism : This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it take different number of arguments and can do different task..

<?php
interface Vehicle {
	public function name();
}

class Car implements Vehicle {
	public function name() {
		echo "I am a Car";
	}
}

class Bus implements Vehicle {
	public function name() {
		echo "I am a Bus";
	}
}

function test(Vehicle $vehicle) {
	$vehicle->name();
}

test(new Car()); // I am a Car
test(new Bus()); // I am a Bus

?>

Overloading : A type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.[/vc_column_text][/vc_column][/vc_row]

On: 03-06-2020