Page 178 - Open Soource Technologies 304.indd
P. 178
Web Technologies-I
Notes
Introduction
Basic object-oriented programming functionality was added in PHP 3 and improved in PHP 4.
In previous versions of PHP, objects were handled like value types. The drawback of this method
was that the whole object was copied when a variable was assigned or passed as a parameter
to a method. In the new approach, objects are referenced by handle, and not by value. PHP 5
introduced private and protected member variables and methods, along with abstract classes
and final classes as well as abstract methods and final methods. It also introduced a standard
way of declaring constructors and destructors, similar to that of other object-oriented languages
such as C++, and a standard exception handling model. Furthermore, PHP 5 added interfaces
and allowed for multiple interfaces to be implemented. There are special interfaces that allow
objects to interact with the runtime system. Objects implementing ArrayAccess can be used
with array syntax and objects implementing Iterator or Iterator Aggregate can be used with the
foreach language construct. There is no virtual table feature in the engine, so static variables
are bound with a name instead of a reference at compile time.
Object is the instantiate of a class. A class consists of a member variable and methods. In PHP
we need to declare the access specifiers (public, private, or protected).
Now we have an example on PHP class.
<?php
class A
{
public function disp(){
echo “Inside the class<br/>”;
}}
$a=new A();
$a->disp();
A::disp();
?>
Output:
Inside the class
Inside the class
8.1 The Basics of Objects
In the real world we think of objects as real entities: a car, gate, or a light bulb. These
entities can do things—cars can drive, gates can open or close and light bulbs can emit
light. But there is more to this than meets the eye, because not only can objects do things,
they have properties. A car can be travelling at a certain speed, and in a certain direction.
A gate can be open, or closed, or busy closing or opening. A light bulb can be on or off, be
emitting light at a certain temperature, be consuming electricity at a certain rate relative to
its temperature and wattage.
In PHP we can define objects in similar ways; we can assign properties to them as well as make
them do things programmatically. Obviously these objects will only exist in the program itself, but
172 LOVELY PROFESSIONAL UNIVERSITY