0% found this document useful (0 votes)
51 views

PHP Oop

This document discusses object-oriented programming (OOP) in PHP. It explains that PHP is an interpreted scripting language that has supported OOP features since version 5. The key aspects of OOP in PHP covered include defining classes with methods and properties, instantiating objects from classes, accessing object properties and methods, using constructors and destructors, and implementing inheritance through the extends keyword. Classes allow grouping related code into reusable objects, and inheritance allows deriving new classes from existing classes.

Uploaded by

Vijay Anand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

PHP Oop

This document discusses object-oriented programming (OOP) in PHP. It explains that PHP is an interpreted scripting language that has supported OOP features since version 5. The key aspects of OOP in PHP covered include defining classes with methods and properties, instantiating objects from classes, accessing object properties and methods, using constructors and destructors, and implementing inheritance through the extends keyword. Classes allow grouping related code into reusable objects, and inheritance allows deriving new classes from existing classes.

Uploaded by

Vijay Anand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

PHP

OBJECT ORIENTED APPROACH


Introduction

PHP is a scripting language


 A scripting language is a form of programming language that is
usually interpreted rather than compiled.
 Conventional programs are converted permanently into executable files before
they are run.
 Scripting language are interpreted one command at a time.

PHP 4 supported only few features of OOP

PHP 5 provides full support for OOP

When to go for OOP??


OOP with PHP

Objects:

 Code grouped by common theme

 Abstractions of code structure


 Methods
 Attributes
OOP with PHP

Defining classes:
 Classes are defined using the keyword ‘class’
Ex: class Person {
……..
}
Defining methods:
 Methods are defined using the keyword ‘function’
Ex: class Person {
function sayHello() {
…………………
}
}
OOP with PHP

Defining attributes/properties/instance variables


 The attributes of the class must be defined using the keyword
‘var’
 The attributes can also defined by using one of the access
modifiers ‘public’, ’protected’ or ‘private’
 It is mandatory that either ‘var’ or access modifiers are used to
define attributes
Ex: class Person {
var name;
var leg_count = 2;//Attributes can be initialized directly
………..
}
OOP with PHP

 Instantiating a class:
 Instantiating a class means creating an object of type of a given
class
 Each object of a class has it’s own identity

 An object in PHP is created using the keyword ‘new’

Ex: <?php
class Person {
………………
}
$person = new Person();
?>
OOP with PHP

Calling methods and accessing attributes using:


 Methods and attributes can be accessed using objects of the
class with the operator ‘->’
Ex: <?php class Person {
private $name = “VJ”;
function getName() {
return $name;
}
}
$person = new Person();
$person->getName();
?>
OOP with PHP

Referencing an instance:
 Reference assignment is automatic for objects in PHP 5
 Same is not true in case of PHP 4 [Must use ‘&’ explicitly]

Ex: // In PHP 5
$img = new Image();
$img2 = $img; // Now, imag2 references imag

// In PHP 4
$img = new Image();
$img2 =& $img;
OOP with PHP

Referencing an instance using ‘this’:


 ‘this’ is pseudo variable that references the instance of class
which is responsible for invocation of the class member
Ex: <?php
class Robot {
function sayHello() {
echo “This line is from class ”.get_class($this);
}
}
new Robot()->sayHello();
?>
OOP with PHP

Using constructors and destructors:


 Constructors and destructors are special methods that are
automatically called whenever a object is created or destroyed
 Constructors:
 Constructors is special method which is invoked when an object is
created
 It performs initialization operations
OOP with PHP

Constructor example:
class Bar {
     public function Bar() {
          // treated as constructor in PHP 5.3.0-5.3.2
         // treated as regular method as of PHP 5.3.3
     }
}

class Bar {
    function __construct() {
        // Constructors in PHP 5.3.3 and above
    }
}
OOP with PHP

 Destructors
 Destructors are usually used to deallocate memory and do other
cleanup operations
 A destructor is called for a class object when that object passes out
of scope or is explicitly deleted
Ex:
class Bar {
function __destruct() {
…...........
}
}
OOP with PHP

Class inheritance:

 Creating or deriving a new class using another class as a base is


called inheritance

 The new class created is called a Derived class and the old
class used as a base is called a Base class

 The derived class will inherit all the features of the base class
OOP with PHP

Inheritance in PHP

 The inheritance of class in PHP is achieved through the


keyword ‘extends’

 All features of base class are inherited by the derived class,


expect for the features defined as private

 Multiple inheritance of class is not allowed in PHP


OOP with PHP

Example:
<php
class Foo {
function sayThis() {
echo “This is in Foo class”;
}
}
class Bar extends Foo {
}
$foo = new Foo();
$bar = new Bar();
$foo->sayThis();
$bar->sayThis();
?>
THANK YOU
Resources,
http://www.php.net/manual/en/oop5.intro.php
http://www.lynda.com/

You might also like