Prev | Current Page 48 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

Let's see the example below:
//test.dbmanager.php
include_once("class.dbmanager.php");
$dbdriver = DBManager::getMySQLDriver();
//now process db operation with this $dbdriver object
?>
Notice that we didn't create any instance of DBManager object like
$dbmanager = new DBManager(). Rather we directly access one of its methods
using the :: operator.
So how does this benefit us? Well, we just need a driver object, so no need to create a
new DBManager object and commit it to memory as long as our scripts are executing.
Static methods usually perform a specific task and finish it.
Here are some important things to note. You can't use $this pseudo object inside
a static method. As the class is not instantiated, $this doesn't exist inside a static
method. You should rather use the self keyword.
Let's take a look at the following example. It shows how a static property
actually works:
//class.statictester.php
class StaticTester
{
private static $id=0;
function __construct()
{
self::$id +=1;
}
public static function checkIdFromStaticMehod()
{
echo "Current Id From Static Method is ".self::$id."\n";
}
public function checkIdFromNonStaticMethod()
{
echo "Current Id From Non Static Method is ".


Pages:
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60