Programming Basics
  Home arrow Programming Basics arrow Page 3 - Beginning Object Oriented Programming ...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
PROGRAMMING BASICS

Beginning Object Oriented Programming in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 132
    2003-11-11

    Table of Contents:
  • Beginning Object Oriented Programming in PHP
  • Objects - The Magic Box
  • Classes
  • Methods
  • Extends
  • 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


    Beginning Object Oriented Programming in PHP - Classes


    (Page 3 of 6 )

    A class is technically defined as a representation of an abstract data type. In laymen's terms a class is a blueprint from which new will construct our box. It's made up of variables and functions that allow our box to be self-aware. With the blueprint, new can build our box exactly to our specifications.

    <?php
    class Box
    {
      var 
    $contents;

      function 
    Box($contents) {
        
    $this-&gt;contents $contents;
      }

      function 
    get_whats_inside() {
        return 
    $this-&gt;contents;
      }
    }

    $mybox = new Box("Jack");
    echo 
    $mybox-&gt;get_whats_inside();
    ?>

    A closer look at our blueprint shows it contains the variable $contents which is used to remember the contents of the box. It also contains two functions: Box and get_whats_inside.

    When the box springs into existence, PHP will look for and execute the function with the same name as the class. That's why our first function has the same name as the class itself. The whole purpose of the Box function is to initialize the contents of the box.

    The special variable $this is used to tell Box that contents is a variable that belongs to the whole class and not the function itself. The $contents variable only exists within the scope of the function Box. $this->contents is a variable which was defined as part of the overall class.

    The function get_whats_inside returns the value stored in the class' contents variable, $this->contents.

    When the entire script is executed the class Box is defined, new constructs a box and passes "Jack" to it's startup function. The initialization function, which has the same name as the class itself, accepts the value passed to it and stores it within the class's variable so that it's accessible to functions throughout the entire class.

    Now we've got a nice, shiny new Jack in the Box.

    More Programming Basics Articles
    More By bluephoenix


       · the tutorial is very clear &gt;&gt;really excellent
       · I liked the article.Does PHP4 support private/protected/public variables and...
       · I just got a good handle on how functions work and with this it just elevates to...
       · one of best explains i'v seen about objects..sorry for the english ...i speak...
       · DEAR SIR,I WANT TO KNOW ABOUT FUNCTIONS AND OOPS CONCEPTS IN PHP PLEAS REPLY TO MY...
       · thanx for the tutorial.. now I know how to use Class.....
       · Thanks for writing such a clear and concise introduction to OOP.I now feel...
       · This is a great article.I have never read such a great article on OOP in my life.The...
       · Nice article to understatnd OOPS. The English used to explain OOPS is very simple to...
       · I couldn't make it work until I made some changes, then I ended up doing some...
       · Fantastic tutorial! Well spoken!
       · your website displays displays greater than signs ( > ) as > and ampersands as...
       · I would like to thank you for writing such an excellent, concise and clear PHP OOP...
       · Very good tutorial very easy to understand and the analogies were excellent
       · In the code examples where text is rendered literally there is no need for the...
       · So, now I understand OOP a little better, can you explain the action of -> using...
       · I'm guessing you are a windows fan. I'd like to pass on to your many, many...
       · I have read lots of them and this is the first I have ever commented on. It was easy...
       · This was a very basic explanation and potentially helpful to anyone who knows the...
       · this is very very very wst knlwdg of OOPS where is orgi basics of OOPS
       · Excellent turtorial on PHP OOP concepts for a beginning PHP procedural...
       · n i must say...ur mind blowing!!!u made my some chunk of time ..quite...
       · Google adds covered the text of the tutorial on one page and covered part of the...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





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