Home arrow PHP arrow Page 4 - Classes as PHP Functions

Constructor Functions - PHP

A class is a function of PHP that has its roots in object oriented programming. The ability to use classes in PHP has been increasing with later versions. If you want to add the power of classes to your PHP programming, keep reading.

TABLE OF CONTENTS:
  1. Classes as PHP Functions
  2. Creating a Class
  3. Adding an Attribute to the Objects
  4. Constructor Functions
By: Jacques Noah
Rating: starstarstarstarstar / 136
August 08, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: