Programming Basics

  Home arrow Programming Basics arrow Page 3 - Beginning Object Oriented Programming ...
PROGRAMMING BASICS

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

    Table of Contents:
  • Beginning Object Oriented Programming in PHP
  • Objects - The Magic Box
  • Classes
  • Methods
  • Extends
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates

     



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