Home arrow PHP arrow Page 4 - Object-Oriented Programming Through Design Patterns

Static (or Class) Attributes and Methods - PHP

If you're looking for an overview of the new object-oriented features of PHP 5, you've found a good place to start. This article, the first of several parts, is excerpted from chapter two of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).

TABLE OF CONTENTS:
  1. Object-Oriented Programming Through Design Patterns
  2. Introduction to OO Programming
  3. Inheritance
  4. Static (or Class) Attributes and Methods
By: Sams Publishing
Rating: starstarstarstarstar / 16
September 21, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Sams Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: