Course Banner


php7

PHP OBJECT-ORIENTED

‘Small talk’ was the first oop language

Java was the first popular Object oriented programing language

C++, java and c# are some of The popular programing languages

Object oriented feature came in php after version 5

OOPS Concept

Any programing language is said to be an OOPs( Object Oriented Programing System) programing language if it has following basic features:

  1. Classes: A group of object which have common properties and behaviour, is called class.
  2. Objects : Object is an example of class.
  3. Abstraction : Highlighting essential features is called Abstraction.
  4. Encapsulation: Hiding unnecessary details is said to be encapsulation. Abstraction is implemented through encapsulation
  5. Inheritance : The feature, in which base class inherits the feature of child class, is called inheritance.
  6. Polymorphism : When same entity behaves differently in different situation, is called polymorphism.

Understanding OOps

Abstraction

client-side-validatiion.jpg

Abstraction is the mechanism in which only the essential features are highlighted and unnecessary detail are hidden from users. For example, in a car the users are provided gear, accelerator, steering wheel etc . User does not need to know how the engine works . How the petrol is converted into electric etc. In programing world, software developer provides the user the user interface to handle the system. User need not know how the coding was done to work the system.

Encapsulation

client-side-validatiion.jpg

Encapsulate, to hide unnecessary details. For example In a capsule, the patient need not know what is inside the capsule. The patient sees the shinny capsule and does not know the bitter medicine which has been encapsulated.

In software world, database connection, security related code is encapsulated from the users so that it cannot be tempered with.

Polymorphism

client-side-validatiion.jpg

Take example of India women. She takes cares of he household work, she takes care of cooking, she takes care of children, she takes care of her office going husband. Etc. She is a single person but does different work as per situation

In Software world, when function name is same for but performs different task as per parameter, its called method overloading or polymorphism.

Inheritance

client-side-validatiion.jpg

The process in which the child class inherits the data member of base class, its called inheritance.

Class

client-side-validatiion.jpg

When you think about a car, you think about whole general car. The car have some attributes and performs task. Its small whecile, its private man carrying whecile, it has four wheels, steering wheel etc. And it can run and honk etc.

So, all the cars having the above said common features will come under car class. So, class is a group of object which has common properties and behaviour(functions).

PHP Class Object

client-side-validatiion.jpg

So, the car class Wireframe Model does not exist physically. Its just imaginary. This is just prototype.

client-side-validatiion.jpg

client-side-validatiion.jpg

client-side-validatiion.jpg

What is a Class?

A group of objects which have common properties and behaviour, is called class.

client-side-validatiion.jpg

So, lets get back to practical PHP world and write a simple code for a class.

Class Car{
	$color=‘Red’ // Example of properties
	$design:string=‘New’ // Example of properties

	public function run(){ // This is example of behaviour(Work)
		echo  ‘Car can run’;
	}
}

What is an Object?

Object is an instance/example of class. Object is physical and we can touch it and work with it.

client-side-validatiion.jpg

client-side-validatiion.jpg

To run the car , the car must exists. We cannot drive with wireframe paper model car. Similarly, to access properties and methods of the class, we must create object first, only then we can access its behaviour.

Class Car{
	$color=‘Red’ // Example of properties
	$design=‘New’ // Example of properties

	public function run(){ // This is example of behaviour(Work)
		echo ‘Car can run’;
	}
}

Here we have created one object of car Class. So, we can access now properties and methods of car class.

echo obj.color;

echo obj.run();

The above properties and methods we can access through its object.

What is constructor?

Constructor is a special function which is automatically called when the object is created. To access other functions we have to create object first and then we can access the function specifically. But constructor can be called as soon as you create object.

PHP Class Inheritance

client-side-validatiion.jpg

client-side-validatiion.jpg

All Cars Inherits from same car Class but some cars have different design and additional functionalities.

client-side-validatiion.jpg

For Example, BMW’s car inherits all features of a normal car but its design is different from normal car and also it have AI features. For example, automatic break, finger touch door open etc.

Types of inheritance

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multi-Level Inheritance

Single Inheritance

client-side-validatiion.jpg

Multiple Inheritance

client-side-validatiion.jpg

Multi-Level Inheritance

client-side-validatiion.jpg

PHP Static Keyword

When data member of any class is declared static, no object is needed to create to access these data members.Points to remember :

  1. In Java and C# there is concept of static class.
  2. In C# , Java static class cannot be inherited.
  3. The difference between Static and Variable is that the variable data is overwritten during program execution while if the data member is Static, it holds the previous values during program execution.
  4. To access static data member, you don’t need to create object of he class, you can directly access these data members using Class Name.

PHP Access Specifier

There are different level of data member accessing rules. So, with this example, I will try to explain how that works.

client-side-validatiion.jpg

At airport of railway station, any user can use the WIFI. That means the wifi can be accessed by anyone.

client-side-validatiion.jpg

All the family members can access the wifi in any certain room. So, in this scenario, we can say, the Home Wifi has ‘Protected Access’

client-side-validatiion.jpg

Your mobile can access internet only within your mobile and not outside.
So, in this scenario, we can say this is ‘’ Private Access”.

client-side-validatiion.jpg

Method Overriding

When base class and child class have same function name then child class overrides base class function and displays the child class function. This activity is called method overriding.

Method Overloading

When function’s name is same but their signature(parameters) is different then its called method Overloading.

When same function performs different task based on parameters, can be termed as function overloading.

client-side-validatiion.jpg

Software Used

  1. Xampp with latest PHP 7
  2. Bootsrap

Pre-requisite

  1. Knowledge of Core PHP.
  2. Basic knowledge of CSS and Bootstrap.

Whom is this tutorial for?

  1. Who has basic knowledge of PHP.
  2. Wants to learn Advanced Object-Oriented CRUD Operation.

Steps to be followed

  1. Create Database
  2. Create table
  3. Insert Form
  4. View List
  5. Edit page
  6. Update Page
  7. Delete Page