PHP
  Home arrow PHP arrow Page 4 - 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 - Static (or Class) Attributes and Methods


    (Page 4 of 4 )

    In addition, methods and properties in PHP can also be declared static. A static method is bound to a class, rather than an instance of the class (a.k.a., an object). Static methods are called using the syntax ClassName::method(). Inside static methods, $this is not available.

    A static property is a class variable that is associated with the class, rather than with an instance of the class. This means that when it is changed, its change is reflected in all instances of the class. Static properties are declared with the static keyword and are accessed via the syntax ClassName::$property. The following example illustrates how static properties work:

    class TestClass {
    public static $counter;
    }
    $counter = TestClass::$counter;

    If you need to access a static property inside a class, you can also use the magic keywords self and parent, which resolve to the current class and the parent of the current class, respectively. Using self and parent allows you to avoid having to explicitly reference the class by name. Here is a simple example that uses a static property to assign a unique integer ID to every instance of the class:

    class TestClass {
    public static $counter = 0;
    public $id;
    public function _ _construct() 
    {
    $this->id = self::$counter++;
    }
    }
    Special Methods

    Classes in PHP reserve certain method names as special callbacks to handle certain events. You have already seen _ _construct(), which is automatically called when an object is instantiated. Five other special callbacks are used by classes: _ _get(), _ _set(), and _ _call() influence the way that class properties and methods are called, and they are covered later in this chapter. The other two are _ _destruct() and _ _clone().

    _ _destruct() is the callback for object destruction. Destructors are useful for closing resources (such as file handles or database connections) that a class creates. In PHP, variables are reference counted. When a variable's reference count drops to 0, the variable is removed from the system by the garbage collector. If this variable is an object, its _ _destruct() method is called.

    The following small wrapper of the PHP file utilities showcases destructors:

    class IO {
    public $fh = false;
    public function _ _construct($filename, $flags)
    {
    $this->fh = fopen($filename, $flags);
    }
    public function _ _destruct()
    {
    if($this->fh) {
    fclose($this->fh);
    }
    }
    public function read($length)
    {
    if($this->fh) {
    return fread($this->fh, $length);
    }
    }
    /* ... */
    }

    In most cases, creating a destructor is not necessary because PHP cleans up resources at the end of a request. For long-running scripts or scripts that open a large number of files, aggressive resource cleanup is important.

    In PHP4, objects are all passed by value. This meant that if you performed the following in PHP4:

    $obj = new TestClass;
    $copy = $obj;

    you would actually create three copies of the class: one in the constructor, one during the assignment of the return value from the constructor to $copy, and one when you assign $copy to $obj. These semantics are completely different from the semantics in most other OO languages, so they have been abandoned in PHP5.

    In PHP5, when you create an object, you are returned a handle to that object, which is similar in concept to a reference in C++. When you execute the preceding code under PHP5, you only create a single instance of the object; no copies are made.

    To actually copy an object in PHP5, you need to use the built-in _ _clone() method. In the preceding example, to make $copy an actual copy of $obj (and not just another reference to a single object), you need to do this:

    $obj = new TestClass;
    $copy = $obj->_ _clone();

    For some classes, the built-in deep-copy _ _clone() method may not be adequate for your needs, so PHP allows you to override it. Inside the _ _clone() method, you not only have $this, which represents the new object, but also $that, which is the object being cloned. For example, in the TestClass class defined previously in this chapter, if you use the default _ _clone() method, you will copy its id property. Instead, you should rewrite the class as follows:

    class TestClass {
    public static $counter = 0;
    public $id;
    public $other;
    public function _ _construct()
    {
    $this->id = self::$counter++;
    }
    public function _ _clone() 
    {
    $this->other = $that->other;
    $this->id = self::$counter++;
    }
    }

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 3 hosted by Hostway
    Stay green...Green IT