PHP
  Home arrow PHP arrow Page 4 - 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 - Constructor Functions
    ( Page 4 of 4 )

     

    This "problem" requires us to write special function called a "constructor function." A constructor function is a function that is written in a way that is very similar to a method of a class. The difference is that a constructor function will be called every time a new instance is made.

    The syntax of a constructor function is:

    function class_name(){
    statements
    }

    Yes, the constructor function name must be the same as the class name. And within the function you can include all the attributes that are going to be included when an instance of the class is made. So, let's add a constructor function that will sort out our problem:

    <?
    class human{
    function human($hcolor){
           $this->hcolor=$hcolor;
    }
    var $legs=2;
    function report(){
    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 " $this->hcolor=$hcolor;" line will install the hcolor variable as an attribute when a new object is instantiated. So, instead of creating the hair color attribute every time we instantiate a new object, it is automatically called. The new class will look like this:

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

    Conclusion

    This article covered the basics of using functions and classes to make coding faster by cutting out code repetition. In the next part we will focus on inheritance, where one class inherits the attributes of another class.



     
     
    >>> 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 3 Hosted by Hostway
    Stay green...Green IT