PHP
  Home arrow PHP arrow Page 2 - Object-Oriented Programming Through De...
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

Object-Oriented Programming Through Design Patterns
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 12
    2006-09-21

    Table of Contents:
  • Object-Oriented Programming Through Design Patterns
  • Introduction to OO Programming
  • Inheritance
  • Static (or Class) Attributes and Methods

  • 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


    Object-Oriented Programming Through Design Patterns - Introduction to OO Programming


    (Page 2 of 4 )

    It is important to note that in procedural programming, the functions and the data are separated from one another. In OO programming, data and the functions to manipulate the data are tied together in objects. Objects contain both data (called attributes or properties) and functions to manipulate that data (called methods).

    An object is defined by the class of which it is an instance. A class defines the attributes that an object has, as well as the methods it may employ. You create an object by instantiating a class. Instantiation creates a new object, initializes all its attributes, and calls its constructor, which is a function that performs any setup operations. A class constructor in PHP5 should be named __constructor() so that the engine knows how to identify it. The following example creates a simple class named User, instantiates it, and calls its two methods:

    <?php
    class User {
    public $name;
    public $birthday;
    public function __construct($name, $birthday)
    {
    $this->name = $name;
    $this->birthday = $birthday;
    }
    public function hello() 
    {
    return "Hello $this->name!\n";
    }
    public function goodbye()
    {
    return "Goodbye $this->name!\n";
    }
    public function age() {
    $ts = strtotime($this->birthday);
    if($ts === -1) {
    return "Unknown";
    }
    else {
    $diff = time() - $ts;
    return floor($diff/(24*60*60*365)) ;
    }
    }
    }
    $user = new User('george', '10 Oct 1973');
    echo $user->hello();
    echo "You are ".$user->age()." years old.\n";
    echo $user->goodbye();
    ?>

    Running this causes the following to appear:

    Hello george!
    You are 29 years old.
    Goodbye george!

    The constructor in this example is extremely basic; it only initializes two attributes, name and birthday. The methods are also simple. Notice that $this is automatically created inside the class methods, and it represents the User object. To access a property or method, you use the -> notation.

    On the surface, an object doesn't seem too different from an associative array and a collection of functions that act on it. There are some important additional properties, though, as described in the following sections:

    • Inheritance—Inheritance is the ability to derive new classes from existing ones and inherit or override their attributes and methods.

    • Encapsulation—Encapsulation is the ability to hide data from users of the class.

    • Special Methods—As shown earlier in this section, classes allow for constructors that can perform setup work (such as initializing attributes) whenever a new object is created. They have other event callbacks that are triggered on other common events as well: on copy, on destruction, and so on.

    • Polymorphism—When two classes implement the same external methods, they should be able to be used interchangeably in functions. Because fully understanding polymorphism requires a larger knowledge base than you currently have, we'll put off discussion of it until later in this chapter, in the section "Polymorphism."

    More PHP Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Advanced PHP Programming," published by...
     

    Buy this book now. This article is excerpted from chapter two of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616). Check it out today at your favorite bookstore. Buy this book now.

       

    PHP ARTICLES

    - 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
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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