Thursday, May 12, 2011

Overloading in PHP

more info.. http://evergreenphp.blogspot.com

Overloading
Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic
entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms "inaccessible properties" and "inaccessible methods" to refer to this combination of declaration and visibility.
All overloading methods must be defined as public.
Note: None of the arguments of these magic methods can be passed by reference.
Note: PHP's interpretation of "overloading" is different than most object oriented
languages. Overloading traditionally provides the ability to have multiple methods with the
same name but different quantities and types of arguments.
Changelog
Version
Description
5.3.0
Added__callStati c(). Added warning to enforce public visibility and non-static declaration.
5.1.0
Added__iss et() and__unset().
Property overloading
void__s et ( string$name , mixed$value )
mixed__get ( string$name )
bool__iss et ( string$name )
void__un s et ( string$name )
__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.
__isset() is triggered by calling isset() or empty() on inaccessible properties.
__unset() is invoked when unset() is used on inaccessible properties.
The$nam e argument is the name of the property being interacted with. The__s et() method's$value
argument specifies the value the$nam e'ed property should be set to.
Property overloading only works in object context. These magic methods will not be triggered in static
context. Therefore these methods can not be declared static.
Note: The return value of __set() is ignored because of the way PHP processes the
assignment operator. Similarly,__get() is never called when chaining assignemnts together
like this:
$a = $obj->b = 8;
data[$name]= $value;
}public function__get($name) {
echo "Getting '$name'\n";
if (array_key_exists($name,$this->data)) {
return$this->data[$name];
}$trace= debug_backtrace();
trigger_error(
'Undefined property via __get(): '. $name .
' in ' .$trace[0]['file'].
' on line ' .$trace[0]['line'],
E_USER_NOTICE);
returnnull;
}/** As of PHP 5.1.0 */
public function__isset($name) {
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}/** As of PHP 5.1.0 */
public function__unset($name) {
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}/** Not a magic method, just here for example. */
public functiongetHidden() {
return$this->hidden;
}
}echo"< pre>\n";

$obj = new PropertyTest;
$obj->a= 1;
echo$obj->a ."\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo"\n";
echo$obj->declared ."\n\n";
echo "Let's experiment with the private property named 'hidden':\n";
echo "Privates are visible inside the class, so __get() not used...\n
";
echo$obj->getHidden() ."\n";
echo "Privates not visible outside of class, so __get() is used...\n"
;echo$obj->hidden ."\n";
?>
Method overloading
mixed__cal l ( string$name , array$arguments )
mixed__cal lStatic ( string$name , array$arguments )
__call() is triggered when invoking inaccessible methods in an object context.
__callStatic() is triggered when invoking inaccessible methods in a static context.
The$nam e argument is the name of the method being called. The$ a rg um en ts argument is an
enumerated array containing the parameters passed to the$nam e'ed method.
runTest('in object context');
MethodTest::runTest('in static context');// As of PHP 5.3.0
?>

No comments: