What is OOPs?
OOPs stands for Object Oriented Programming System
It is system independent of any programming language with a goal “better project management”.
OOPs is made of four core components
1. Encapsulation
2. Abstraction
3. Polymorphism
4. Inheritance
These core components also require three other components
1. class
2. object or instance
3. reference
What is class?
A class is a set of specifications or blueprint about an entity describing all possible data members and the methods to be applicable on that entity.
Use class keyword to make a class.
class <entityname>
{
}
A class can have different types of components
1. data members
2. methods (functions)
a. Use function keyword to define a function
3. constructor
a. Used to initialize data in data members
4. setter and getter
a. setters are used to update some data in data members
b. getters are used to return some data in data members
Example
Create a class Customer having data members as cid, name, balance.
Create a constructor to initialize data in the data members.
Create setter methods to update the data in data members
Create getter methods to return the values in data members
Create a method deposit() to deposit some amount
Create a method withdraw() to withdraw some method
Create a sample record for a customer with data 123, “Rakesh”,5000
Deposit 3000 more
Withdraw 1500
Show the output in following format
Balance of <name> with customer id <cid> is <balance>
Note:
Use var keyword to declare the data members
What is constructor?
Special method inside a class with same name as class name and no return type
Use this keyword to refer the data members when data member name and the argument names are same
Use arrow operator -> to access the data members
What is an instance or object?
A real entity created based on class specification is called as instance.
Use new keyword to create an instance
Use arrow operator -> to access the data members and the methods
Full Code
<?php
class Customer
{
var $cid,$name,$balance; //data members
function Customer($cid,$name,$balance) //constructor
{
$this->cid=$cid;
$this->name=$name;
$this->balance=$balance;
}
function setName($name) //setter
{
$this->name=$name;
}
function getCid() //getter
{
return $this->cid;
}
function getName()
{
return $this->name;
}
function getBalance()
{
return $this->balance;
}
function deposit($amount) //general method
{
$this->balance=$this->balance+$amount;
}
function withdraw($amount)
{
$this->balance=$this->balance-$amount;
}
}
?>
<?php
$c=new Customer(123,"Rakesh Kumar",5000); //creating instance -- here $c is called as reference
$c->deposit(3000); //calling a funciton using reference
$c->withdraw(1500);
echo "Balanace of ".$c->getName()." with customer id ".$c->getCid()." is ".$c->getBalance()."<br>";
$c->setName("Rakesh Chaudhary");
echo "Balanace of ".$c->getName()." with customer id ".$c->getCid()." is ".$c->getBalance();
?>
Types of Members of a class
A class can have two types of members
1. static or class members
2. non-static or instance members
The class members get called using a class name without need of an instance or reference. It requires another operator called scope resolution operator :: to call such members
Syntax
classname::functioname()
Use static keyword with such functions
The instance members needs an instance and its reference and use arrow operator -> to call such members
$ref=new classname();
$ref->functioname();
No keyword is required with such members
Example of static member
Create a class General having a function factorial() which takes some number as argument and returns factorial of that number
<?php
class General
{
static function factorial($n)
{
$f=1;
for($i=1;$i<=$n;$i++)
$f=$f*$i;
return $f;
}
}
echo General::factorial(5);
?>
Example of non-static member
Create a class General having a data member as num. Create a constructor to initalize data in num.
Create function factorial() to show factorial of num.
<?php
class General
{
var $num;
function General($num)
{
$this->num=$num;
}
function factorial()
{
$f=1;
for($i=1;$i<=$this->num;$i++)
$f=$f*$i;
return $f;
}
}
$g=new General(6);
$result=$g->factorial();
echo $result;
?>
A class can have two types of members
1. static or class members
2. non-static or instance members
The class members get called using a class name without need of an instance or reference. It requires another operator called scope resolution operator :: to call such members
Syntax
classname::functioname()
Use static keyword with such functions
The instance members needs an instance and its reference and use arrow operator -> to call such members
$ref=new classname();
$ref->functioname();
No keyword is required with such members
Example of static member
Create a class General having a function factorial() which takes some number as argument and returns factorial of that number
<?php
class General
{
static function factorial($n)
{
$f=1;
for($i=1;$i<=$n;$i++)
$f=$f*$i;
return $f;
}
}
echo General::factorial(5);
?>
Example of non-static member
Create a class General having a data member as num. Create a constructor to initalize data in num.
Create function factorial() to show factorial of num.
<?php
class General
{
var $num;
function General($num)
{
$this->num=$num;
}
function factorial()
{
$f=1;
for($i=1;$i<=$this->num;$i++)
$f=$f*$i;
return $f;
}
}
$g=new General(6);
$result=$g->factorial();
echo $result;
?>
This comment has been removed by the author.
ReplyDelete