Searching...
Sunday 28 July 2013

More components of Object Oriented PHP


Static Class Members

In the articles on properties and methods in classes in PHP namely:



You were made acquainted with the declarations and scope of properties and methods in classes in PHP (more about classes in PHP). But the Static scope of properties and methods was not introduced there to prevent confusion for the newbies. Confusion? Not really! It’s just for the sake of convenience. Now that you are comfortable with class’s components I can introduce Static Scope here.

One of the major benefits to using static properties is that they keep their stored values for the duration of the script.

Sometimes it’s useful to create properties and methods that are not invoked by any particular object but rather are pertinent to and are shared by all class instances.

It is similar to static variables discussed in article: Variables in PHP.

For example, suppose that you are writing a class that tracks the number of web page visitors. You wouldn’t want the visitor count to reset to zero every time the class is instantiated, so you would set the property to be of the static scope:

<?php
class Visitor
{
private static $visitors = 0;
function __construct()
{
self::$visitors++;
}
static function getVisitors()
{
return self::$visitors;
}
}
// Instantiate the Visitor class.
$visits = new Visitor();
echo Visitor::getVisitors()."<br />";
// Instantiate another Visitor class.
$visits2 = new Visitor();
echo Visitor::getVisitors()."<br />";
?>

The results are as follows:

1
2

Because the $visitors property was declared as static, any changes made to its value (in this case via the class constructor) are reflected across all instantiated objects. Also note that static properties and methods are referred to using the self keyword and class name, rather than via $this and arrow operators. This is because referring to static properties using the means allowed for their “regular” siblings is not possible and will result in a syntax error if attempted.

Note: You can’t use $this within a class to refer to a property declared as static.

The instanceof Keyword

The instanceof keyword was introduced with PHP 5. With it you can determine whether an object is an instance of a class, is a subclass of a class, or implements a particular interface, and do something accordingly. For example, suppose you want to learn whether $manager is derived from the class Employee:

$manager = new Employee();
...
if ($manager instanceof Employee) echo "Yes";

Note: The class name is not surrounded by any sort of delimiters (quotes). Including them will result in a syntax error.

The instanceof keyword is particularly useful when you’re working with a number of objects simultaneously. For example, you might be repeatedly calling a particular function but want to tweak that function’s behavior in accordance with a given type of object. You might use a case statement and the instanceof keyword to manage behavior in this fashion.


0 comments:

Post a Comment

 
Back to top!