Learn

Class : 

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

ex-> 

<?php
class Rahul {
public $name ;
private $lastname;
protected $fullname ;

function Name() // method
{
echo “Rahul”;

}
}

$name = new Rahul();
$name->Name();

?>

Object :

An individual instance of the data structure defined by a class . You  defines a class once and then make many object that belongs to it . Object are also know as instance.

Example 1 ->

class Example {
public $name ;
public $item ;

function Sample(){

$this->test();
}

function Test(){

echo ‘Test’;
echo $this->item;
$regular = 500;
echo $regular ;
}

}

$e = new Example() ;
$e->Sample();

?>

Example  2 ->

<?php
class A
{
private $a;
function set($a)
{
$this->a=$a;
}
function disp()
{
echo $this->a.”<br>”;
}

};
$obj=array(new A(),new A,new A(),new A(),new A());

$data=array(10,20,30,40,50);

for($i=0;$i<count($obj);$i++)
{
$obj[$i]->set($data[$i]);
}

foreach($obj as $v)
$v->disp();

?>

Inheritance :

When a class is defined by inheriting existing function of a parent class then it is called inheritance . Here child class will inherit all or few member function and variables of a parent class.

Example ->

<?php

class Foo
{
public function printItem($string)
{
echo ‘Foo: ‘ . $string . PHP_EOL;
}

public function printPHP()
{
echo ‘PHP is great.’ . PHP_EOL;
}
}

class Bar extends Foo
{
public function printItem($string)
{
echo ‘Bar: ‘ . $string . PHP_EOL;
}
}

$foo = new Foo();
$bar = new Bar();
$foo->printItem(‘baz’); // Output: ‘Foo: baz’
$foo->printPHP();       // Output: ‘PHP is great’
$bar->printItem(‘baz’); // Output: ‘Bar: baz’
$bar->printPHP();       // Output: ‘PHP is great’

?>

Ploymorphism : 

This is a object oriented  concept where same function can be used for different purpose,

Example ->

<?php

class lineitem {
var $amount,$what;
function lineitem ($amount,$desc) {
$this->amount = $amount;
$this->what = $desc;
}

function getname () {
return $this->what;
}

function getval () {
return $this->amount;
}

}

class bill extends lineitem {
function bill ($amount,$desc) {
$this->amount = -$amount;
$this->what = $desc;
}

}

class income extends lineitem {

}

$acc[0] = new bill(15.40,”Break Rolls”);
$acc[1] = new bill(17.75,”Carpet leaner”);
$acc[2] = new income(37.84,”options added by Berks Folks”);

$tot = 0;
$format = “%-35s … %8.2f\n”;

foreach ($acc as $item) {
$amount = $item->getval();
$whom = $item->getname();
printf($format, $whom,$amount);
$tot += $amount;
}

printf($format, “TOTAL …”,$tot);

?>

OUTPUT:

DerivedClass method called

In the above example, object $c of class DerievedClass is executed and passed to the processClass() method. The parameter accepted in processClass() is that of BassClass. Within the processClass() the method myMethod() is being called. Since the method is being called on the class variable of BaseClass, it would not be wrong to assume that myMethod() of class BaseClass will be called. But, as per the definition “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism”, myMethod() will be called on object DerievedClass. The reason why this happens is because the object of DerievedClass is being passed and hence the method myMethod() of DerievedClass will be called.

function name will remain same but it make different number of arguments and can do different task.

Overloding :

Overloading is ability of one function to perform different tasks, i.e,it allows creating several methods with the same name which differ from each other in the type of the input and the output of the function.
Overloading can be used with functions and members.

Example ->

int volume(int s),
double volume(double r,int h),
long volume(long l,int b,int h)

Depending on the parameter passed the corresponding function will be called.

Data Abstraction : 

Any representation of data in which the implementation data are hidden(abstracted) is known as data abstraction.

Example – >

<?php

abstract class Cheese
{
//can ONLY be inherited by another class
}

class Cheddar extends Cheese
{
}

$dinner = new Cheese; //fatal error
$lunch = new Cheddar; //works!

?>

Encapsulation : 

Refers To a concept where we encapsulate all the data and member function together to form object.

 

Advertisements

Blog at WordPress.com.

Up ↑

Little Local

littlelocal

playwithlifeorg

4 out of 5 dentists recommend this WordPress.com site

Crafsol Technology Solutions

Crafting Enterprise Services

Rahul Rajoria

Sr. Software Developer(Looking for a opportunity)

system care

best laptop repairing center in indore