Tải bản đầy đủ (.ppt) (23 trang)

Slide môn học PHP session 8b OOP concepts

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (278.23 KB, 23 trang )

OOP Concepts
Session 21


Review








Functionality of the email process is done
with the help of the mail() function
Main arguments of the mail() function are:
 Recipient’s email address
 Subject of the email
 Body of the email
Local mail server for mail() function is
specified in the php.ini configuration file
Type of messages are specified with the
help of the content-type header while
attaching a file to the email
PHP / Session 21 / Slide 2 of 23


Objectives


Discuss Object Oriented Basics





Use Inheritance



Use Classes



Use Constructors



Create and Use Objects

PHP / Session 21 / Slide 3 of 23


Object Oriented Basics






Programming with a data type of a data
structure that is defined with the type of
functions and are performed on the data

structure
Uses for combining the data and the functions
into a single unit or an object
Concept of an object-oriented programming are:
 Objects
 Class
 Abstraction
 Encapsulation
 Polymorphism
 Inheritance
PHP / Session 21 / Slide 4 of 23


Inheritance - I








Creates a new class with the help
of a base class
Properties
such
as
variables,
functions, and methods of the base
class are transferred to the newly

derived class
Uses
the
extends
keyword
for
inheriting a new class from the
base class
Derived class also has its own
variables and functions
PHP / Session 21 / Slide 5 of 23


Inheritance - II


Types of inheritances are:






Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance

Note: In PHP, multiple inheritances

are not supported

PHP / Session 21 / Slide 6 of 23


Class








Object that contains variables and
functions of different data types is a
class
Class can be inherited from a base class
to create a new derived class
Derived class uses all the properties of
the base class including its own
properties
Class variables are local to its class
PHP / Session 21 / Slide 7 of 23


Declaring a Class


Syntax for declaring a class is:

class class_name
{
var class_variable;
function function_name()
}

Where,







class – Uses for declaring a class name
class_name – Specifies the class name
class_variable – Specifies the variable name for
the class
function – Uses for defining a function
function_name() – Specifies the function name
PHP / Session 21 / Slide 8 of 23


Class - Example- I


For example, to enter the
employee details using classes
class

{
var
var
var
var

empdetail
$empid;
$empname;
$empdepart;
$empdesign;

PHP / Session 21 / Slide 9 of 23


Class - Example- II
function enteremp($id, $name)
{
$this->empid=id;
$this->empname=name;
}
function enterdet($depart, $design)
{
$this->empdepart=depart;
$this->empdesign=design;
}
}
?>

PHP / Session 21 / Slide 10 of 23



Saving the Class file




Saves the file as empdet.inc and
includes it in the .php file
Includes the empdet.inc file in
the .php file in the following
format:
include “empdet.inc”;

PHP / Session 21 / Slide 11 of 23


Inheriting a Class




A new class can be inherited from an
old class
A new class uses the properties of
the parent class along with its own
properties

PHP / Session 21 / Slide 12 of 23



Syntax for Inheriting a Class


Syntax for inheriting a new class
is:
class class_name
{
var class_variable;
function function_name()
}
class new_class extends class_name
{
var class_variable;
function function_name()
}
PHP / Session 21 / Slide 13 of 23


Inheritance - Example - I


Derive a gross_salary class from the salary class
class salary
{
function sal_hra($basic)
{
return $basic * 0.25;
}

function sal_ta($basic)
{
return $basic * 0.08;
}
PHP / Session 21 / Slide 14 of 23


Inheritance - Example - II
function sal_pf($basic)
{
return $basic * 0.05;
}
}
class gross_salary extends salary
{
function gross($basic,$hra,$ta,$pf)
{
return $basic + ($hra + $ta) - $pf;
}
}
?>
PHP / Session 21 / Slide 15 of 23


Constructors







Function that has the same name as
that of its class name is a
constructor
Uses new operator for calling
constructor in the main program. That
is creating a request for the class
Provides values to all the objects of
the class when a constructor is
declared
PHP / Session 21 / Slide 16 of 23


Creating a Constructor


Syntax for creating a constructor is:
class class_name
{
var class_variable;
function constructor_name()
}

Where,








class – Uses for declaring a class name
class_name – Specifies the class name
class_variable – Specifies the variable name for the
class
function – Uses for defining a function
constructor_name() – Specifies the function name
that is same as the class name

PHP / Session 21 / Slide 17 of 23


Constructor - Example - I


For example, to call a constructor from a
derived class from a base class
class string
{
function string()
{
echo “This function is made a constructor.”;
}
function stringdisp()
{
echo “This is an example for constructor.”;
}
}
PHP / Session 21 / Slide 18 of 23



Constructor - Example - II
class display extends string
{
function displaystring()
{
echo “This is a new class and a new function”;
}
}
$disp = new display;
echo $disp;
?>

PHP / Session 21 / Slide 19 of 23


Create and Use Objects










Self-contained entity those consists of both
data and procedures to manipulate the data. It

is an instance of a class
Uses for maintaining the codes in the program.
Objects can be manipulated as needed
Provides values to all the objects of the class
when a constructor is declared
Gives reference to a class. There can be more
than one object for a single class
Uses the new operator to initialize the object
PHP / Session 21 / Slide 20 of 23


Creating an Object


Syntax for creating an object is:
class class_name
{
var class_variable;
function function_name()
}
$object_name = new class_name;

Where,





object_name – Specifies the name of
object

new – Initializes the object
class_name – Specifies the class name

the

PHP / Session 21 / Slide 21 of 23


Creating an Object - Example


For example, create an object for the class
usermail
class usermail
{
var $username = “john”;
var $password = “abc123”;
}
class userdetails extends usermail
{
var $secretquery = “favourite food”;
var $answer = “chinese”;
}
$mail = new userdetails;
echo $mail;
?>
PHP / Session 21 / Slide 22 of 23



Summary












OOP is a programming language model, made up of objects and
data. Objects can be manipulated as needed
Main characteristics of an OOP are objects, class,
abstraction, encapsulation, polymorphism, and inheritance
Class is an object that can be inherited from a base class
to a derived class. It has its own properties
Deriving a new class from a parent class or a base class is
termed as inheritance. It is done with the help of the
extends keyword
Function that has the same name as that of its class name
is a constructor. It is called by using a new operator
Object is an instance of a class. It gives a reference to
the class

PHP / Session 21 / Slide 23 of 23




×