PHP
  Home arrow PHP arrow Page 4 - Object Oriented Programming in PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Object Oriented Programming in PHP
By: Luis Argerich
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 184
    2003-01-13

    Table of Contents:
  • Object Oriented Programming in PHP
  • Data Members and Functions
  • Constructors
  • Overloading
  • OOP Programming in PHP
  • Copying and Cloning
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Object Oriented Programming in PHP - Overloading


    (Page 4 of 7 )

    Overloading (which is different from overriding) is not supported in PHP. In OOP you "overload" a method when you define two/more methods with the same name but different number or type of parameters (depending upon the language).

    PHP is a loosely typed language so overloading by types won't work, however overloading by number of parameters doesn't work either.

    It's very nice sometimes in OOP to overload constructors so you can build the object in different ways (passing different number of arguments). A trick to do something like that in PHP is:

    <?php

    class Myclass {
    function Myclass() {
    $name="Myclass".func_num_args();
    $this->$name();
    //Note that $this->$name() is usually wrong but here
    //$name is a string with the name of the method to call.
    }

    function Myclass1($x) {
    code;
    }
    function Myclass2($x,$y) {
    code;
    }
    }

    ?>


    With this extra working in the class the use of the class is transparent to the user:

    $obj1=new Myclass('1'); //Will call Myclass1
    $obj2=new Myclass('1','2'); //Will call Myclass2


    Sometimes this is very nice.

    Polymorphism

    Polymorphism is defined as the ability of an object to determine which method to invoke for an object passed as argument in runtime time. For example if you have a class figure which defines a method draw and derived classes circle and rectangle, where you override the method draw you might have a function which expects an argument x and then call $x->draw().

    If you have polymorphism the method draw called depends of the type of object you pass to the function.

    Polymorphism is very easy and natural in interpreted languages as PHP (try to imagine a C++ compiler generating code for this case, which method do you call? You don't know yet which type of object you have! OK, this is not the point).

    So PHP happily supports polymorphism.

    <?php

    function niceDrawing($x) {
    //Supose this is a method of the class Board.
    $x->draw();
    }

    $obj=new Circle(3,187);
    $obj2=new Rectangle(4,5);

    $board->niceDrawing($obj); //will call the draw method of Circle.
    $board->niceDrawing($obj2); //will call the draw method of Rectangle.

    ?>

    More PHP Articles
    More By Luis Argerich


       · Thanks for the article.Just wanted to correct a typo..to make the internet a...
       · This article is pretty old, it needs updation for PHP version 5 and later.
       · This OOPs is very helpful me, but i expects with author to make it update plz for...
       · :rofl:PHP is not an OOP language in the first place. It's meant to be simple and...
       · Obviously the good people from PHP.net thought it was the right way to go seeing as...
     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT