0% found this document useful (0 votes)
22 views23 pages

PHP With Oops

The document provides an overview of object-oriented programming (OOP) concepts, including classes, objects, inheritance, constructors, destructors, and exception handling in PHP. It explains how classes serve as templates for creating objects, how inheritance allows for code reuse, and how exceptions can be managed using try, throw, and catch blocks. Additionally, it includes code examples to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views23 pages

PHP With Oops

The document provides an overview of object-oriented programming (OOP) concepts, including classes, objects, inheritance, constructors, destructors, and exception handling in PHP. It explains how classes serve as templates for creating objects, how inheritance allows for code reuse, and how exceptions can be managed using try, throw, and catch blocks. Additionally, it includes code examples to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CCCS - 415

Advanced Web Development Using


Objects of Real World
• We can imagine our universe made of
different objects like sun, earth, moon etc.
• Similarly we can imagine our car made of
different objects like wheel, steering, gear etc.
• Same way there is object oriented
programming concepts which assume
everything as an object and implement a
software using different objects.
Object Oriented Concepts
• Class − This is a programmer-defined data
type, which includes local functions as well as
local data. You can think of a class as a
template for making many instances of the
same kind (or class) of object.
Object Oriented Concepts
• Member Variable − These are the variables
defined inside a class. This data will be
invisible to the outside of the class and can be
accessed via member functions. These
variables are called attribute of the object
once an object is created.
Object Oriented Concepts
• Member function − These are the function
defined inside a class and are used to access
object data.
Object Oriented Concepts
• Object − An individual instance of the data
structure defined by a class. You define a class
once and then make many objects that belong
to it. Objects are also known as instance.
Object Oriented Concepts
• 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 functions and
variables of a parent class.
Object Oriented Concepts
• Parent class − A class that is inherited from by
another class. This is also called a base class or
super class.
Object Oriented Concepts
• Child Class − A class that inherits from another
class. This is also called a subclass or derived
class.
<?php
Class Syntax
class Fruit function set_color($color)
{ {
public $name; $this->color = $color;
public $color; }
function set_name($name)
{ function get_color()
$this->name = $name; {
} return $this->color;
function get_name() }
{ }
return $this->name;
} $this is a reserved keyword in PHP that refers to the calling
$apple = new Fruit(); -> is referred to as
the object operator, or
$banana = new Fruit();
sometimes the single
$apple->set_name('Apple'); arrow operator. It is an
$banana->set_name('Banana'); access operator used for
access/call methods and
echo $apple->get_name();
properties in a PHP object
echo "<br>"; in Object-Oriented
echo $banana->get_name(); Programming (OOP).
echo "<br>"; $kiwi = new Fruit();
echo "<br>"; $kiwi->set_name('Kiwi');
$kiwi->set_color('Olive Green');
echo "Name: " . $kiwi->get_name();
echo "<br>";
echo "Color: " . $kiwi->get_color();
echo "<br>"
Object Oriented Concepts
• Constructor − refers to a special type of
function which will be called automatically
whenever there is an object formation from a
class.
Object Oriented Concepts
• Destructor − refers to a special type of
function which will be called automatically
whenever an object is deleted or goes out of
scope.
Constructor Functions
• Constructor Functions are special type of
functions which are called automatically
whenever an object is created. So we take full
advantage of this behavior, by initializing many
things through constructor functions.
• PHP provides a special function
called __construct() to define a constructor.
You can pass as many as arguments you like
into the constructor function.
Example
function __construct( $par1, $par2 )
{
$this->title = $par1;
$this->price = $par2;
}
<?php
class Books { Constructor Example
/* Member variables */ $physics = new Books( "Physics for
var $price; High School", 10 );
var $title;
$maths = new Books ("Algebra", 7 );
function __construct( $par1,
$par2 ) { $chemistry = new Books ( "Advanced
$this->title = $par1; Chemistry", 15 );
$this->price = $par2;
}
function getPrice() { $physics->getTitle();
echo $this->price ."<br/>"; $chemistry->getTitle();
} $maths->getTitle();
function getTitle( ){ $physics->getPrice();
echo $this->title ." <br/>"; $chemistry->getPrice();
} $maths->getPrice();
} ?>
Destructor
• Like a constructor function you can define a
destructor function using
function __destruct().
• You can release all the resources with-in a
destructor.
• Destructor automatically call at last.
• Note : _ _destruct() is used to define
destructor.
<?php
class demo
{ Destructor Example
function __construct()
{
echo "object is initializing their
properties"."<br/>";
}
function work()
{
echo "Now works is going "."<br/>";
}
function __destruct()
{
echo "after completion the
work, object destroyed automatically";
}
}
$obj= new demo();

$obj->work();
?>
Inheritance
• Inheritance is one of the most important
aspects of OOP. It allows a class to inherit
members from another class.
• It helps in code reuse and code reduction.
• We declare a new class with additional
keyword extends.
• Note : PHP only supports multilevel
inheritance.
class empl extends person
Example {
<?php var $desg;
class person var $comp;
{ var $comp_cont;
var $name; }
var $add; $emp = new empl();
var $phone; echo $emp->name=“QWERTY"."</br>";
echo $emp->add=“XYZ"."</br>";
}
echo $emp->phone="789"."</br>";
echo $emp->desg=“DY.ER"."</br>";
echo $emp->comp=“ABCD"."</br>";
echo $emp->comp_cont="123"."</br>";
?>
Exception
Exception handling is used to change the
normal flow of the code execution if a specified
error (exceptional) condition occurs. This
condition is called an exception.
This is what normally happens when an exception
is triggered:
•The current code state is saved
•The code execution will switch to a predefined
(custom) exception handler function
•Depending on the situation, the handler may
then resume the execution from the saved code
state, terminate the script execution or continue
the script from a different location in the code
Try, throw and catch
To avoid the error from the example above, we need
to create the proper code to handle an exception.
Proper exception code should include:
try - A function using an exception should be in a
"try" block. If the exception does not trigger, the code
will continue as normal. However if the exception
triggers, an exception is "thrown"
throw - This is how you trigger an exception. Each
"throw" must have at least one "catch"
catch - A "catch" block retrieves an exception and
creates an object containing the exception
information
<?php
//create function with an exception
function checkNum($number) {
if($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try {
checkNum(1);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below’;
}e
//catch exception
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>

You might also like