Monday, January 4, 2010

Static Keyword in PHP

Static Keyword

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).
Static member example
class Foo
{
    public static 
$my_static 'foo';
    public function 
staticValue() {
        return 
self::$my_static;
    }
}
class 
Bar extends Foo
{
    public function 
fooStatic() {
        return 
parent::$my_static;
    }
}
print 
Foo::$my_static "\n";
$foo = new Foo();
print 
$foo->staticValue() . "\n";
print 
$foo->my_static "\n";      // Undefined "Property" my_static
print $foo::$my_static "\n";
$classname 'Foo';
print 
$classname::$my_static "\n"// As of PHP 5.3.0
print Bar::$my_static "\n";
$bar = new Bar();
print 
$bar->fooStatic() . "\n";
?>

Static method example
class Foo {
    public static function 
aStaticMethod() {
        
// ...
    
}
}
Foo::aStaticMethod();
$classname 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>

Class Abstraction in PHP

Class Abstraction

PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation.
When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
Abstract class example
abstract class AbstractClass {
    // Force Extending class to define this method
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);

    // Common method
    public function printOut() {
        print $this->getValue() . "\n";
    }
}
 class ConcreteClass1 extends AbstractClass {
    protected function getValue() {
        return "ConcreteClass1";
    }
     public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}
 class ConcreteClass2 extends AbstractClass {
    public function getValue() {
        return "ConcreteClass2";
    }
     public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass2";
    }
}

$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";

$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n"
;
?>

The above example will output:
ConcreteClass1
FOO_ConcreteClass1
ConcreteClass2
FOO_ConcreteClass2
Old code that has no user-defined classes or functions named 'abstract' should run without modifications.

Constructors in PHP

Constructors

Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class will be called, if it exists.
class Auto_Cart extends Cart {
    function 
Auto_Cart() {
        
$this->add_item("10"1);
    }
}
?>

This defines a class Auto_Cart that is a Cart plus a constructor which initializes the cart with one item of article number "10" each time a new Auto_Cart is being made with "new". Constructors can take arguments and these arguments can be optional, which makes them much more useful. To be able to still use the class without parameters, all parameters to constructors should be made optional by providing default values.
class Constructor_Cart extends Cart {
    function 
Constructor_Cart($item "10"$num 1) {
        
$this->add_item ($item$num);
    }
}

// Shop the same old boring stuff.
$default_cart = new Constructor_Cart;

// Shop for real...
$different_cart = new Constructor_Cart("20"17);
?>

You also can use the @ operator to mute errors occurring in the constructor, e.g. @new.
class {
    function 
A() {
        echo 
"I am the constructor of A.\n";
    }
     function 
B() {
        echo 
"I am a regular function named B in class A.\n";
        echo 
"I am not a constructor in A.\n";
    }
}

class 
extends { }

// This will call B() as a constructor
$b = new B;
?>

The function B() in class A will suddenly become a constructor in class B, although it was never intended to be. PHP 4 does not care if the function is being defined in class B, or if it has been inherited.


Destructors are functions that are called automatically when an object is destroyed, either with unset() or by simply going out of scope. There are no destructors in PHP. You may use register_shutdown_function() instead to simulate most effects of destructors.