PHP
  Home arrow PHP arrow Page 3 - Classes as PHP Functions
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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

Classes as PHP Functions
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 85
    2006-08-08


    Table of Contents:
  • Classes as PHP Functions
  • Creating a Class
  • Adding an Attribute to the Objects
  • Constructor Functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Classes as PHP Functions - Adding an Attribute to the Objects
    ( Page 3 of 4 )

    Let' create another instance to see what happens:

    <?
    class human{
    var $legs=2;
    var $arms=2;
    }
    //instantiate the class
    < style="color: navy;" lang="EN-GB">$jude = new human();
    < style="color: navy;" lang="EN-GB">$jane = new human();
    < style="color: red;" lang="EN-GB">$jude-
    >legs++; //increase legs by one
    echo "Jude has " .$jude->legs." Legs<br>";
    echo "Jane has " .$jane->legs." legs";
    ?>

    ...the result..

     

    As you can see from the result, we can modify the instance of a class without touching the class definition itself. This re-usability is what makes classes so useful.

    Now we can also add a new attribute to any of the objects (jane or jude). For example, let's add a new attribute to jane. The attribute is hair color:

     <?
    class human{
    var $legs=2;
    var $arms=2;
    }
    //instantiate the class
    $jude = new human();
    $jane = new human();
    < style="color: red;" lang="EN-GB">$jane->haircolor="brown";
    $jude->legs++; //increase legs by one
    echo "Jude has " .$jude->legs." Legs<br>";
    echo "Jane has ".$jane->haircolor." Hair and was created from the
    class <b>".get_class($jane)."</b> She also has <b>".$jane->legs."
    </b>legs";
    ?>

    So if we wanted to do the same thing for jude, we have to type all that out again, which is time consuming. Instead, we are going to automate this process by creating a function that will report on both jane and jude. So let's change the class to include this function:

    <?
    class human{
    var $legs=2;
    < style="color: red;" lang="EN-GB">function report(){
    < style="color: red;" lang="EN-GB">return  "This <b>".get_class
    ($this)."</b> has <b>" .$this->haircolor. "</b> hair,and
    <b>" .$this->legs. "</b> legs<br>" ;
    }
    }
    //instantiate the class
    $jude = new human();
    $jane = new human();
    $jane->haircolor="brown";
    $jude->haircolor="black";
    $jude->legs++; //increase legs by one
    echo $jane->report();
    echo $jude->report();
    ?>

    The variable $this enables us to refer to the object without knowing its name. Remember, when we created the class, we had no idea that an object called "jane" or "jude" was going to be created further along the line.

    We said in our previous discussion that classes have attributes and methods, well, the function "report" is our method.

    As you can see from the result above, the report() function has automated the process of reporting the attributes of an object. But we have a problem: not all humans have the same hair color, so we can't declare the hair color as an attribute of the class, and we can't create a hair color attribute each time we create a new "human" object, because it will make the code inefficient. So what can we do?



     
     
    >>> More PHP Articles          >>> More By Jacques Noah
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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