Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Unit 3 PHP

Object Oriented Concept in PHP

Uploaded by

Mahesh Pokharkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit 3 PHP

Object Oriented Concept in PHP

Uploaded by

Mahesh Pokharkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Object Oriented Concept in PHP

Creating class and Object


• A class is a template for objects which contain properties
and methods.
• A class is defined by using the class keyword, followed by
the name of the class and a pair of curly braces ({}).
• All its properties and methods go inside the braces:
• Syntax:
<?php
class Class_Name
{
// code goes here...
}
?>
Class Example
<?php
class Fruit
{
// Properties
public $name;
public $color;
// Methods
function set_name($name)
{
$this->name = $name;
}

function get_name()
{
return $this->name;
}
}
?>
Object
• an object is an instance of class.
• We can create multiple objects from a class.
• Each object has all the properties and
methods defined in the class, but they will
have different property values.
Example
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}

function get_name() {
return $this->name;
}
}
$apple = new Fruit();
$banana = new Fruit();
?>
Constructor
• A constructor allows you to initialize an object's
properties upon creation of the object.
• If you create a __construct() function, PHP will
automatically call this function when you create
an object from a class.
• construct function starts with two underscores
(__)
• using a constructor saves us from calling the
set_name() method which reduces the amount of
code
Example
<?php
class Fruit
{
public $name;
public $color;
function __construct($name)
{
$this->name = $name;
}

function get_name()
{
return $this->name;
}

}
$apple = new Fruit("Apple");
echo $apple->get_name();
?>
destructor
• A destructor is called when the object is destructed or
the script is stopped or exited.
• If you create a __destruct() function, PHP will
automatically call this function at the end of the script.
• Notice that the destruct function starts with two
underscores (__)!
• The example below has a __construct() function that is
automatically called when you create an object from a
class, and a __destruct() function that is automatically
called at the end of the script
Example
<?php
Output:
class Fruit
{
public $name; Apple
public $color; The fruit is Apple.
function __construct($name)
{
$this->name = $name;
}

function __destruct()
{
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
echo $apple->name,"<br>";
?>
Inheritance
• When a class derives from another class is
called Inheritance.
• 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.
Example
<?php // Strawberry is inherited from Fruit
class Fruit
{ class Strawberry extends Fruit
public $name; {
public $color; public function message()
{
public function __construct($name,$color) echo "Am I a fruit or a berry? ";
{ }
$this->name = $name;
$this->color = $color; }
} $strawberry
public function intro() = new Strawberry("Strawberry", "red");
{ $strawberry->message();
echo "The fruit is {$this->name} and the $strawberry->intro();
color is {$this->color}."; ?>
}
}
Output
Am I a fruit or a berry? The fruit is Strawberry
and the color is red.

You might also like