Overloading
Overloading in PHP provides means to dynamically "create"
members 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
members 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
members" 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.
Member overloading
void
__set (
string $name ,
mixed
$value )
mixed __get
( string
$name )
bool __isset
( string
$name )
void
__unset (
string $name )
__set() is run when writing data to inaccessible
members.
__get() is utilized for reading data from
inaccessible members.
__isset() is triggered by calling isset() or empty()
on inaccessible members.
__unset() is invoked when unset() is used on
inaccessible members.
The $name argument is the name of
the member being interacted with. The __set() method's
$value argument specifies the value the
$name'ed member should be set to.
Member overloading only works in object context. These magic
methods will not be triggered in static context. Therefore these methods can
not be declared static.
Example #1 overloading with __get, __set, __isset and __unset example
<?php
class
MemberTest {
private $data
= array();
public $declared
= 1;
private $hidden
= 2;
public
function __set($name,
$value) {
echo "Setting '$name' to '$value'\n";
$this->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: '
. $name
.
' in '
. $trace[0]['file']
.
' on line '
. $trace[0]['line'],
E_USER_NOTICE);
return null;
}
public
function __isset($name)
{
echo "Is '$name' set?\n";
return isset($this->data[$name]);
}
public
function __unset($name)
{
echo "Unsetting '$name'\n";
unset($this->data[$name]);
}
public
function getHidden()
{
echo "'hidden' visible here so
__get() not used\n";
return $this->hidden;
}
}
echo "<pre>\n";
$obj = new
MemberTest;
$obj->a
= 1;
echo $obj->a
. "\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo $obj->declared
. "\n";
echo $obj->getHidden()
. "\n";
echo $obj->hidden
. "\n";
?>
The above example will output:
Setting 'a' to '1'
Getting 'a'
1
Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)
1
'hidden' visible here so __get() not used
2
Getting 'hidden'
Notice: Undefined property: hidden in <file> on line 64 in <file> on line 28
Method overloading
mixed __call
( string
$name ,
array $arguments
)
mixed __callStatic
( 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 $name argument is the name of
the method being called. The $arguments argument
is an enumerated array containing the parameters passed to the
$name'ed method.
Example #2 overloading instantiated methods with __call and ___callStatic
<?php
class
MethodTest {
public
function __call($name,
$arguments)
{
echo "Calling object method '$name' "
. implode(',
',
$arguments). "\n";
}
public
static function __callStatic($name,
$arguments)
{
echo "Calling static method '$name' "
. implode(',
',
$arguments). "\n";
}
}
$obj = new
MethodTest;
$obj->runTest('in
object context');
MethodTest::runTest('in
static context');
?>
The above example will output:
Calling object method 'runTest' in object context
Calling static method 'runtest' in static context