Wednesday, 31 January 2018

OOPS Concepts In PHP

Class and Object
Class is a collection of objects.
Class is generic, whereas Object is specific.

Class defines properties/functions of an Object.

Object is an instance of a Class and it has its own state, behavior and identity.

You can instantiate an object, but not a Class
Example 1: if computer is a class then keyboard, mouse are the objects.
Example 2: if fruits are class then mango, orange, apple are the objects.
Class:
Class consists of properties and methods.
Example:
Class Demo {

        public $name = “LearningPoint92”;

        public function display() {
                echo $name;
        }
}
 
 
 To instantiate an object of a class, use the keyword new as below:
$obj =new Demo ();
To access method, write the code below
$obj-> display();
Inheritance
Inheritance is a concept where one class shares the structure and behavior defined in another class.
Child class can access the methods and properties of parent class.
Parent class can access only its own method and properties.
Parent class is called as base class and super class.
Child class is called as derived class and sub class.

extends keyword is used  in inheritance.
Advantages: code reusability, extensibility.
Example:
class Fruit {
        public function eat() {
                echo "nice taste";
        }
}

class Apple extends Fruit {
  public function show() {
      echo "wow </br>";
        }
      }
$obj = new Apple();
$obj->show();
$obj->eat();
Output:
wow
nice taste
Polymorphism:
Polymorphism is nothing but assigning behavior or value in a child class to something that was already declared in the parent class.

Polymorphism takes more than one form (poly means many and morphism means forms).

Overloading:Same method name with different signature is called as method overloading and Php doesnot support method overloading concept.

Overriding:Same  name with same signature is called as overriding. 

Method overriding:

Same method name with same signature is called as method overriding. 

It occurs in different classes(when same method name defined in both parent and child class,Overriding occurs).


Example:
 class A

{

function display($a,$b)

{

$res=$a*$b;

echo "Multiplication = ".$res;

}

}


class child extends A

{

function display($a,$b)

{

$res=$a+$b;

echo "Sum  = ".$res;

}

}

 $obj= new child();

 $obj->display(1000,500);

Output:
Sum=1500
Encapsulation
Encapsulation means wrapping or hiding of data in a class and controlling its access.

Encapsulation is implemented using private, package-private and protected access modifier.

Example:

Mobiles and owners.
all the functions of mobiles are encapsulated with the owners.
No one can access it.

Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them.
Advantages: Security
Example:
class Student
 {

private $age;

public function setAge($ag)

{
 $this->age=$ag;
 }

public function getAge()
 {
 return $this->age;
 }
 }

$Stu=new Student();
 $Stu->setAge(25);
 echo "You are ".$Stu->getAge()." years old";

Output: You are 25 years old

abstraction:

Abstraction means providing important or necessary features without giving internal or background details.
Example: 
When user is using Mobile application he doesn't know internal working of all functions like calling, send sms but he can call or send sms without knowing internal details. His main aim is using the mobile phone not to know about internal structure
Visibility
Each method and property has its visibility. There are three types of visibility in PHP. They are declared by keywords public, protected and private
Public: It can access anywhere in the program (that is from outside or inside its method/property).
This is the default visibility in PHP class when no keywords are used in method/property.
Protected: It can access itself or child classes (method/property).

Private: It can access only itself where it is declared (method/property)

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...