Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
6Activity
0 of .
Results for:
No results containing your search query
P. 1
Object Oriented Programming in PHP

Object Oriented Programming in PHP

Ratings: (0)|Views: 178 |Likes:
Published by Tanju YILDIZ

More info:

Published by: Tanju YILDIZ on Aug 10, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/13/2013

pdf

text

original

 
Object Oriented Programming in PHP
 This article introduces Object Oriented Programming (OOP) in PHP. Luis shows youhow to code less and better by using some OOP concepts and PHP tricks.ObjectOriented Programming in any language is the use of objects to represent functionalparts of an application and real life entities. For example you may have a Personobject to hold the data related to a person and even provide some functionality thatthis person may be capable of.Object Oriented Programming has long been used in games to represent theobjects such as a User or an Enemy, or even a Weapon. This amazing way of programming has proven just as useful in software and web development.In my opinion any OOP language should have:
Abstract data types and information hiding
Inheritance
Polymorphism This can all be done using PHP classes:
<?phpclass Something {// In OOP classes are usually named starting with a cap letter.var $x;function setX($v) {// Methods start in lowercase then use lowercase to separate// words in the method name example getValueOfArea()$this->x=$v;}function getX() {return $this->x;}}?>
Of course you can use your own nomenclature but having a standardized one isuseful.
Data Members and Functions
Data members are defined in PHP using a "var" declaration inside the class andthey have no type until they are assigned a value. A data member might be aninteger, an array, an associative array or even an object.Methods are defined as functions inside the class, to access data members insidethe methods you have to use
$this->name
, otherwise the variable is local to themethod. You create an object using the new operator:
$obj=new Something;Then you can use member functions like:$obj->setX(5);$see=$obj->getX();
 The setX member function assigns 5 to the x data member in the object obj (notin the class), then getX returns its value; 5 in this case. You can access the data members from the object reference using for example:
$obj->x=6
. However, this is not a very good OOP practice.I encourage you to set data members by defining methods to set them andaccess the data members by using retrieving methods.
 
 You'll be a good OOP programmer if you consider data members inaccessible andonly use methods from the object handler. Unfortunately PHP doesn't have a way todeclare a data member private so bad code is allowed.
Inheritance
Inheritance is easy in PHP using the extends keyword:
<?phpclass Another extends Something {var $y;function setY($v) {// Methods start in lowercase then use uppercase initials to// separate words in the method name example getValueOfArea()this->y=$v;}function getY() {return $this->y;}}?>
Objects of the class "Another" now have all the data members and methods of theparent class (Something) plus its own data members and methods. You can use:
$obj2=new Something;$obj2->setX(6);$obj2->setY(7);
Multiple-inheritance is not supported so you can't make a class extend two ormore different classes. You can override a method in the derived class by redefining it. If we redefinegetX in "Another" we can no longer access method getX in "Something".If you declare a data member in a derived class with the same name as a datamember in a Base class the derived data member "hides" the base class datamember when you access it.
Constructors
 You might define constructors in your classes; constructors are methods with thesame name as the class and are called when you create an object of the class, forexample:
<?phpclass Something {var $x;function Something($y) {$this->x=$y;}function setX($v) {$this->x=$v;}function getX() {return $this->x;}}
 
?>
So you can create an object using:
$obj=new Something(6);
...and the constructor automatically assigns 6 to the data member x.Constructors and methods are normal PHP functions so you can use defaultarguments.
function Something($x="3",$y="5");
 Then:
$obj=new Something(); // x=3 and y=5$obj=new Something(8); // x=8 and y=5$obj=new Something(8,9); // x=8 and y=9
Default arguments are used in the C++ way so you can't pass a value to Y and letX take the default value. Arguments are assigned from left to right and when no morearguments are found if the function expected more they take the default values.When an object of a derived class is created only its constructor is called theconstructor of the Parent class is not called. This is a gotcha of PHP because constructor chaining is a classic feature of OOP, if you want to call the base class constructor you have to do it explicitly from thederived class constructor.It works because all methods of the parent class are available at the derived classdue to inheritance.
<?phpfunction Another() {$this->y=5;$this->Something(); //explicit call to base class constructor.}?>
Abstract Classes
A nice mechanism in OOP is the use of Abstract Classes; abstract classes areclasses that cannot be instantiated and the only purpose is to define an interface forits derived classes.Designers often use Abstract classes to force programmers to derive classes fromcertain base classes, so they can be certain that the new classes have some desiredfunctionality. There's no standard way to do that in PHP but, if you do need this feature, justdefine the base class and put a "die" call in its constructor so you can be sure thatthe base class is never instantiated.Now define the methods (interface) putting "die" statements in each one so if in aderived class a programmer doesn't override the method then an error is raised.Furthermore you might need to be sure, since PHP has no types, that some objectis from a class derived from your base class, then add a method in the base class toidentify the class (return "some id") and verify this when you receive an object as anargument.Of course this doesn't work if the evil programmer overrides the method in thederived class but generally the problem is dealing with lazy programmers not evil

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->