PHP
  Home arrow PHP arrow Page 2 - 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 / 216
    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 - Data Members and Functions


    (Page 2 of 7 )

    Data members are defined in PHP using a "var" declaration inside the class and they have no type until they are assigned a value. A data member might be an integer, an array, an associative array or even an object.

    Methods are defined as functions inside the class, to access data members inside the methods you have to use $this->name, otherwise the variable is local to the method.

    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 (not in 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 and access the data members by using retrieving methods.

    You'll be a good OOP programmer if you consider data members inaccessible and only use methods from the object handler. Unfortunately PHP doesn't have a way to declare a data member private so bad code is allowed.

    Inheritance

    Inheritance is easy in PHP using the extends keyword:

    <?php

    class 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 the parent 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 or more different classes.

    You can override a method in the derived class by redefining it. If we redefine getX 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 data member in a Base class the derived data member "hides" the base class data member when you access it.

    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...
       · another nice way of using persistent objects can be found here...
       · what great news that php-people included some persistent object stuff. I never...
     

    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-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek