Unit 3 - Apply Object Oriented Concepts in PHP
Unit 3 - Apply Object Oriented Concepts in PHP
UNIT – III
APPLY OBJECT ORIENTED
CONCEPTS IN PHP
E-mail: [email protected]
COURSE OUTCOMES (COS)
CO3 - Develop programs by applying various object
oriented concepts.
UO
Sunil P. Emekar
1. Write constructor and destructor functions for the
given problem in PHP.
2. Implement inheritance to extend the given base
class.
3. Use overloading overriding to solve the given
problem.
4. Clone the given object.
2
CREATING CLASSES AND OBJECTS
Sunil P. Emekar
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 −
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.
3
DEFINING PHP CLASSES
A class can be declared using the class keyword,
followed by the name of the class and a pair of curly
braces ({}).
Syntax to Create Class in PHP
Sunil P. Emekar
<?php
class MyClass
{
// Class properties and methods go here
}
?>
4
CREATING OBJECTS
Once a class has been defined, objects can be created
from the class with the new keyword.
Class methods and properties can directly be accessed
through this object instance.
Sunil P. Emekar
Syntax:
<?php
class MyClass
{
// Class properties and methods go here
}
$obj = new MyClass();
?>
The arrow symbol (->) is an OOP construct that is
used to access contained properties and methods of a 5
given object.
In PHP The var_dump() function is used to display the
structured information (type and value) about one or
more variables.
Syntax:
var_dump($obj);
Example:
<?php
Sunil P. Emekar
class demo
{
private $a= "Sunil Emekar";
public function display() {
echo $this->a;
}
}
$obj = new demo();
$obj->display();
var_dump($obj); 6
?>
CONSTRUCTORS
A constructor allows you to initialize an object's
properties upon creation of the object.
We can design constructor using "__construct“ function
construct function starts with two underscores ( _ _ )
Sunil P. Emekar
Example:
<?php
class Example
{
public function __construct()
{
echo "Hello World";
}
}
7
$obj = new Example();
?>
DESTRUCTORS
The destructor method will be called as soon as all
references to a particular object are removed or when
the object is explicitly destroyed.
We create destructor by using "__destruct" function.
Sunil P. Emekar
A destructor is called automatically when a scripts
ends.
However, to explicitly trigger the destructor, you can
destroy the object using the PHP unset() function
8
<?php
class Example
{
public function __construct()
{
echo "Hello World<br>";
Sunil P. Emekar
}
public function __destruct()
{
echo "destroy.....";
}
}
$obj = new Example();
#unset($obj);
9
?>
CONTROLLING THE VISIBILITY OF PROPERTIES
AND METHODS
Sunil P. Emekar
There are three visibility keywords public, protected,
private, which determines how and from where
properties and methods can be accessed and modified.
public — A public property or method can be accessed
anywhere, from within the class and outside. This is the
default visibility for all class members in PHP.
protected — A protected property or method can only be
accessed from within the class itself or in child or inherited
classes i.e. classes that extends that class.
private — A private property or method is accessible only
from within the class that defines it. Even child or
10
inherited classes cannot access private properties or
methods.
INHERITANCE
Sunil P. Emekar
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.
PHP supports only single inheritance, where only
one class can be derived from single parent class.
11
<?php
class A
{
public function display()
{
echo "In Class A <br>";
}
Sunil P. Emekar
}
class B extends A
{
public function view()
{
echo "In Class B";
}
}
$obj= new B();
$obj->display();
$obj->view();
12
?>
PHP’S OVERLOADING
PHP’s interpretation of “overloading” is different than
most object oriented languages.
Overloading traditionally provides the ability to have
multiple methods with the same name but different
Sunil P. Emekar
quantities and types of arguments.
PHP’s Overloading
PHP’s overloading is to create dynamic entities.
Properties and methods are those entities created
dynamically by using PHP overloading.
After creating an object for a class, we can access set
of entities, that are, properties or methods not defined
within the scope of the class.
Such entities are said to be overloaded properties or
13
methods, and the process is called as overloading.
OVERLOADING
Overloading in PHP provides means to dynamically
create properties and methods.
Types of Overloading in PHP:
Property Overloading
Sunil P. Emekar
Method Overloading:
14
Property and Rules of overloading in PHP:
Sunil P. Emekar
defined within the scope of the class.
Such entities are said to be overloaded properties or
methods, and the process is called as overloading.
For working with these overloaded properties or
functions, PHP magic methods are used.
Most of the magic methods will be triggered in
object context except __callStatic() method which is
used in a static context.
15
PROPERTY OVERLOADING
PHP property overloading is used to create dynamic
properties in the object context.
For creating these properties no separate line of code
is needed.
Sunil P. Emekar
A property associated with a class instance, and if it
is not declared within the scope of the class, it is
considered as overloaded property.
Following operations are performed with overloaded
properties in PHP.
Setting and getting overloaded properties.
Evaluating overloaded properties setting.
Undo such properties setting.
Sunil P. Emekar
__get(): It is utilized for reading data from
inaccessible Properties.
__isset(): This magic method is invoked when we
check overloaded properties with isset() function.
__unset(): This function will be invoked on using
PHP unset() for overloaded properties.
17
METHOD OVERLOADING:
The related magic functions are,
__call() – triggered while invoking overloaded
methods in the object context.
Sunil P. Emekar
This is a magic method that PHP calls when it tries to
execute a method of a class and it doesn't find it.
This magic keyword takes in two arguments: a function
name and other arguments to be passed into the function.
Syntax:
function __call(string $function_name, array $arguments) {
}
__callStatic() – triggered while invoking overloaded
methods in static context.
18
<?php
class SampleClass{
function __call($function_name,$arguments){
$count = count($arguments);
if($function_name == "add"){
if($count== 2){
return array_sum($arguments);
}elseif($count == 3){
Sunil P. Emekar
return array_sum($arguments);
}
}
}
}
$object = new SampleClass();
echo $object->add(2,3)."<br>"; // Outputs 5
echo $object->add(2,3,3); // Outputs 8
?>
19
OVERRIDING IN PHP
Sunil P. Emekar
method in child class.
basically the purpose of overriding is to change
the behavior of your parent class method.
20
<?php
class ParentClass {
public function test() {
echo "in parent class";
}
Sunil P. Emekar
}
class ChildClass extends ParentClass {
public function test() {
echo "In child class";
}
}
$objChildClass = new ChildClass;
$objChildClass->test();
21
?>
CLONING OBJECT
Sunil P. Emekar
object.
Example:
$obj = new MyClass();
// Clone the object
$copy = clone $obj;
22
If any of the properties was a reference to
another variable or object, then only the
reference is copied.
Sunil P. Emekar
Objects are always passed by reference, so if the
original object has another object in its
properties, the copy will point to the same object.
This behavior can be changed by creating a
__clone() method in the class.
23
PHP INTROSPECTION
Sunil P. Emekar
With introspection, you can write code that operates
on any class or object.
You don't need to know which methods or properties
are defined when you write your code; instead, you
can discover that information at runtime, which
makes it possible for you to write generic debuggers,
serializers, profilers, etc.
24
INTROSPECTION - EXAMINING CLASSES
To determine whether a class exists, use the class_exists( )
function, which takes in a string and returns a Boolean
value.
you can use the get_declared_classes( ) function, which
returns an array of defined classes and checks if the class
Sunil P. Emekar
name is in the returned array:
$yes_no = class_exists(classname);
$classes = get_declared_classes( );
You can get the methods and properties that exist in a
class (including those that are inherited from superclasses)
using the get_class_methods( ) and get_class_vars( )
functions.
These functions take a class name and return an array:
$methods = get_class_methods(classname);
$properties = get_class_vars(classname);
Use get_parent_class( ) to find a class's parent class: 25
$superclass = get_parent_class(classname);
INTROSPECTION - EXAMINING AN OBJECT
To get the class to which an object belongs, first make
sure it is an object using the is_object( ) function, then get
the class with the get_class( ) function:
$yes_no = is_object(var);
Sunil P. Emekar
$classname = get_class(object);
Sunil P. Emekar
This is useful for storing or passing PHP values
around without losing their type and structure.
To make the serialized string into a PHP value
again, use unserialize().
To serialize data means to convert a value to a
sequence of bits, so that it can be stored in a file, a
memory buffer, or transmitted across a network.
Syntax
27
serialize(value);