PHP
  Home arrow PHP arrow Classes as PHP Functions
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 66
    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:
      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


    Classes as PHP Functions


    (Page 1 of 4 )

    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.

    Introduction

    Continuing our PHP functions article, we move on to creating classes. Let me say right at the start that you can write perfectly effective and useful PHP code without creating classes or going into object oriented programming. Another thing is that PHP, at its core, is not an object oriented language. This is because PHP was built from the C language, which is at its core a procedural language, rather than a methodical one. However, object oriented programming can be very powerful and PHP programmers are increasingly taking advantage of these capabilities, which have been greatly expanded since PHP4.

    What is a class?

    A class is a collection of variables and functions that serves a common purpose. It gives you the ability to think about real world objects and translate them into code. For example, let's try to describe a car. The class "car" might have variables: $name_of_car, $wheels, $steeringwheel, $windscreen, $lights, $pedals and $brakes. The functions of a car might include Turnleft(),Turnright() and Accelerate(). The function "Accelerate()" might take arguments such as $increase_speed. Now, all of the above describes a car and what a car does in both real terms and in code.

    Now you might ask, couldn't  this be done with regular functions and variables? Yes, it could, especially if you were talking about one car. But if you are talking about more than one car, then it would be impossible to keep up with all the various variables and functions associated with multiple cars. This is where classes become very useful, because classes bring all those variables and functions under one name. In this case, it's an object named "car." Now if you have more than one car, all you have to do is instantiate that object. The term instantiate basically means making a copy of the object. The new copy will have all the variables and functions of the original object available to it. You can include the class in any script that you create; the class will work the same.

    Let me sound a cautionary note. Although OOP makes coding easier and more portable, the objects' execution time is less efficient than writing straight code. And while OOP speeds up project development drastically, more often than not, it produces less customized and slower applications. So, be aware of these pitfalls when choosing to use OOP.

    OOP is arguably difficult to learn, so in this article I am going to use very simple examples to demonstrate how to use OOP. This should help new users grasp the concepts easily. Also, because OOP is such a huge topic I will only deal with the basics so as to give you the ability to easily learn the more advanced aspects of it.

    A class has the following members:

    Attributes

    Methods

    A good example of a class is Human. A human class would have characteristics (attributes) of gender, hands, legs, age, and so forth. It would also have actions (methods) such as walking, eating, running, talking, and so on.

    The syntax of a class is as follows:

    class class_name{
    var $variable_name;
    function
    function_name(){
    statements;
    }
    }

    Notice that within the class you use the var keyword to identify your variables. At this point you can also assign a value to your variables. Try to use the same naming conventions as you would for functions. It is easier to recognize what a class is if it reflects its purpose(e.g. class Human_class{). Another keyword, $this, is used to refer to the instances of an object and its attributes e.g. $this->$variable_name. To instantiate a class means to create a new version of it. Say we  have a class called dog, to instantiate it, we do this:

    $rex = new dog();

    Now, rex will have all the attributes of the class dog.

    More PHP Articles
    More By Jacques Noah


       · Hope you enjoyed my article. Any comments are very welcome
       · I was searching all over to understand the classes, your article is the only clear...
       · Nice work on this clear article that not only tells OOP simply, but puts the php...
       · sunquest.devshed advert - totally destroyed the page
       · hi i understand about class and function explanation was clear and example is...
       · I've been digging around trying to figure out what A class is for a few hrs now. I'm...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT