2. What we learn?
• Introduction to Object Oriented Programming
• Classes
• Objects
• Constructor
• destructor
• Introspection
2
Monica Deshmane(Haribhai V. Desai
College,Pune)
3. • Object-oriented programming (OOP)
• refers to the creation of reusable object-types /
classes that can be efficiently developed for multiple
programs.
Monica Deshmane(Haribhai V. Desai
College,Pune)
3
4. • before we go in detail, lets define important terms related to Object
Oriented Programming.
• Class − encapsulation of data members & functions.making many
instances of the class i.e. 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.
• 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.
• Member function − These are the function defined inside a class and are
used to access object data.
• 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 using extends
keyword.
Monica Deshmane(Haribhai V. Desai
College,Pune)
4
5. • Parent class − A class that is inherited from by another class. This is also called a
base class or super class.
• Child Class − A class that inherits from another class. This is also called a subclass
or derived class.
• Polymorphism − same function can be used for different purposes.means one
thing can be expressed in many ways.
• Overloading −(operator overloading) a type of polymorphism in which some or all
of operators have different implementations depending on the types of their
arguments. (function overloading) functions can also be overloaded with different
implementation.
• Data Abstraction − Any representation of data in which the implementation details
are hidden (abstracted).
• Encapsulation − refers to a concept where we bind all the data and member
functions together to form an object.
• Constructor − refers to a special type of function which will be called automatically
whenever there is an object created from a class.
• Destructor − refers to a special type of function which will be called automatically
whenever an object is deleted or goes out of scope.
Monica Deshmane(Haribhai V. Desai
College,Pune)
5
7. • Class is blue print of object
• Class{
Properties or data members
Member functions
}
• Class A
{public $a=2;
Public function f()
{ //code }
}
Monica Deshmane(Haribhai V. Desai
College,Pune)
7
8. • A class definition:
class classname { // classname is a PHP identifier!
// the class body = data & function member definitions
}
• Attributes
– are declared as variables within the class definition
using keywords that match their visibility: public,
private, or protected.
Operations
– are created by declaring functions within the class
definition.
Creating Classes in PHP
8
9. • Three access / visibility modifiers introduced in PHP 5, which
affect the scope of access to class variables and functions:
– public : public class variables and functions can be accessed from
inside and outside the class
– protected : hides a variable or function from direct external class
access + protected members are available in subclasses
– private : hides a variable or function from direct external class access +
protected members are hidden (NOT available) from all subclasses
Monica Deshmane(Haribhai V. Desai
College,Pune)
9
10. Monica Deshmane(Haribhai V. Desai
College,Pune)
10
Class A
{
Public $a=10;
Function f1()
{ //code
}
Function f2()
{ //code
}
}
11. • $this is reserved keyword.
• used to access properties of class.
• Ex.
• Class A
{
Public $a=10;
Function f()
{ echo $this->a; }
}
Monica Deshmane(Haribhai V. Desai
College,Pune)
11
13. • An object is an instance of a class.
• Any number of instances of a class can be created.
Monica Deshmane(Haribhai V. Desai
College,Pune)
13
14. • Create an object of a class = a particular individual
that is a member of the class by using the new
keyword:
$newobj= new ClassName(actual_param_list);
• Notes:
– Class names are case insensitive as are functions
– PHP 5 allows you to define multiple classes in a single script
– Constructor called for initialization of object.
– Destructor called for deletion of object.
Monica Deshmane(Haribhai V. Desai
College,Pune)
14
15. • From operations within the class, class’s data / methods can be
accessed / called by using:
– $this = a variable that refers to the current instance of the class, and can
be used only in the definition of the class, including the constructor &
destructor
– The pointer operator -> (similar to Java’s object member access operator “.” )
– class Test {
public $attribute;
function f ($val) {
$this -> attribute = $val; // $this is mandatory!
} // if omitted, $attribute is treated
} // as a local var in the function
Using Data/Method Members
15
No $ sign here
16. Accesing properties & methods
class Test {
public $var1;
public methodname(parameters){}
}
$t = new Test();
$t->var1 = “value”;
echo $t->var1;
$t->methodname(parameters);
Monica Deshmane(Haribhai V. Desai
College,Pune)
16
17. Constructor: creating object
• Constructor = function used to create an object of the class
– Declared as a function with a special name:
function __construct (param_list) { … }
– Usually performs initialization
– Called automatically when an object is created by new keyword
– A default no-argument constructor is provided by the compiler
only if a constructor function is not explicitly declared in the class
– Cannot be overloaded (= 2+ constructors for a class); if you need a
variable # of parameters, use flexible parameter lists…
Monica Deshmane(Haribhai V. Desai
College,Pune)
17
18. Example:constructor
• <?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();
?>
Monica Deshmane(Haribhai V. Desai
College,Pune)
18
19. Destructor: remove object memory
• Destructor = opposite of constructor
– Declared as a function with a special name,
cannot take parameters
function __destruct () { … }
– Allows some functionality that will be
automatically executed just before an object is
destroyed
An object is removed when there is no reference
variable/handle left to it
Monica Deshmane(Haribhai V. Desai
College,Pune)
19
20. Example:Destructor
• <?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
Monica Deshmane(Haribhai V. Desai
College,Pune)
20
21. • Destructor will be called for 2 ways-
1)When script ends
2)To manually delete object by unset()
Monica Deshmane(Haribhai V. Desai
College,Pune)
21
22. clone
• Clone is keyword.
• Object clone means object holds reference to
another object which it uses.
• When parent is replicated new copy of object
created.
• $copy=clone $obj;
Monica Deshmane(Haribhai V. Desai
College,Pune)
22
24. • Introspection is the ability of a program to examine an
object's characteristics,
• such as its name, parent class (if any), properties, and
methods.
• 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.
Monica Deshmane(Haribhai V. Desai
College,Pune)
24
25. introspective functions provided by PHP.
Monica Deshmane(Haribhai V. Desai
College,Pune)
25
• Get_declared_classes()-
same to check class exists?
• get_class_methods($class);
• get_class_vars($class);
• $classes = get_declared_classes( );
foreach($classes as $class) {}
Class functions-
26. introspective functions provided by PHP.
Monica Deshmane(Haribhai V. Desai
College,Pune)
26
•Is_object()-to check given variable is object or not?
•class_exists() –
checks whether a class has been defined
•get_class() –
returns the class name of an object
•get_parent_class() –
returns the class name of an object’s parent class
•is_subclass_of() –
checks whether an object has a given parent class
• Ex. echo get_class($obj);
Object functions-
27. Example: introspection
Monica Deshmane(Haribhai V. Desai
College,Pune)
27
get_declared_classes( )
function display_classes ( )
{ $classes = get_declared_classes( );
foreach($classes as $class)
{ echo "Showing information about $class";
echo "$class methods:";