文字

重载

PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法。我们是通过魔术方法(magic methods)来实现的。

当调用当前环境下未定义或不可见的类属性或方法时,重载方法会被调用。本节后面将使用"不可访问属性(inaccessible properties)"和"不可访问方法(inaccessible methods)"来称呼这些未定义或不可见的类属性或方法。

所有的重载方法都必须被声明为 public

Note:

这些魔术方法的参数都不能通过引用传递。

Note:

PHP中的"重载"与其它绝大多数面向对象语言不同。传统的"重载"是用于提供多个同名的类方法,但各方法的参数类型和个数不同。

更新日志

版本 说明
5.3.0 新增 __callStatic()魔术方法。可见性未设置为 public 或未声明为 static 的时候会产生一个警告。
5.1.0 新增 __isset() 和 __unset() 两个魔术方法。

属性重载

public void __set ( string $name , mixed $value )
public mixed __get ( string $name )
public bool __isset ( string $name )
public void __unset ( string $name )

在给不可访问属性赋值时,__set() 会被调用。

读取不可访问属性的值时,__get() 会被调用。

当对不可访问属性调用 isset() empty() 时,__isset() 会被调用。

当对不可访问属性调用 unset() 时,__unset() 会被调用。

参数 $name 是指要操作的变量名称。__set() 方法的 $value 参数指定了 $name 变量的值。

属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 PHP 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。

Note:

因为 PHP 处理赋值运算的方式,__set() 的返回值将被忽略。类似的, 在下面这样的链式赋值中,__get() 不会被调用:

$a = $obj->b = 8; 

Note:

在除 isset() 外的其它语言结构中无法使用重载的属性,这意味着当对一个重载的属性使用 empty() 时,重载魔术方法将不会被调用。

为避开此限制,必须将重载属性赋值到本地变量再使用 empty()

Example #1 使用 __get(),__set(),__isset() 和 __unset() 进行属性重载

<?php
class  PropertyTest  {
     

    
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 via __get(): '  $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 () 
    {
        return 
$this -> hidden ;
    }
}


echo 
"<pre>\n" ;

$obj  = new  PropertyTest ;

$obj -> 1 ;
echo 
$obj -> "\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" ;
?>

以上例程会输出:

Setting 'a' to '1'
Getting 'a'
1Is 'a' set?
bool(true)
Unsetting 'a'
Is 'a' set?
bool(false)1Let's experiment with the private property named 'hidden':
Privates are visible inside the class, so __get() not used...
2
Privates not visible outside of class, so __get() is used...
Getting 'hidden'Notice:  Undefined property via __get(): hidden in <file> on line 70 in <file> on line 29

方法重载

public mixed __call ( string $name , array $arguments )
public static mixed __callStatic ( string $name , array $arguments )

在对象中调用一个不可访问方法时,__call() 会被调用。

用静态方式中调用一个不可访问方法时,__callStatic() 会被调用。

$name 参数是要调用的方法名称。 $arguments 参数是一个枚举数组,包含着要传递给方法 $name 的参数。

Example #2 使用 __call() 和 __callStatic() 对方法重载

<?php
class  MethodTest 
{
    public function 
__call ( $name $arguments
    {
        
// 注意: $name 的值区分大小写
        
echo  "Calling object method ' $name ' "
             
implode ( ', ' $arguments ).  "\n" ;
    }

    

    
public static function  __callStatic ( $name $arguments
    {
        
// 注意: $name 的值区分大小写
        
echo  "Calling static method ' $name ' "
             
implode ( ', ' $arguments ).  "\n" ;
    }
}

$obj  = new  MethodTest ;
$obj -> runTest ( 'in object context' );

MethodTest :: runTest ( 'in static context' );   // PHP 5.3.0之后版本
?>

以上例程会输出:

Calling object method 'runTest' in object context
Calling static method 'runTest' in static context

用户评论:

[#1] igorsantos07 [2015-10-03 18:25:18]

As stated in another note, this is a gross misuse of the term "overload" as seen in traditional OOP. It's really bad to see an explanation that "PHP interprets this word differently". The use of __get(), __set(), __call() and friends is actually a technique known elsewhere as "metaprogramming", not overloading.

[#2] Anonymous [2015-04-08 15:34:00]

Using magic methods, especially __get(), __set(), and __call() will effectively disable autocomplete in most IDEs (eg.: IntelliSense) for the affected classes.

To overcome this inconvenience, use phpDoc to let the IDE know about these magic methods and properties: @method, @property, @property-read, @property-write.


class MyClass
{
    private $properties = array('name' => 'IceFruit', 'price' => 2.49)
    
    public function __get($name)
    {
        return $this->properties($name);
    }
}

[#3] gabe at fijiwebdesign dot com [2014-11-27 11:33:29]

Note that you can enable "overloading" on a class instance at runtime for an existing property by unset()ing that property. 

eg: 

<?php
class Test 

    public 
$property1;

    public function 
__get($name
    {
        return 
"Get called for " get_class($this) . "->\$$name \n";
    }

}
?>


The public property $property1 can be unset() so that it can be dynamically handled via __get(). 

<?php 
$Test 
= new Test();
unset(
$Test->property1); // enable overloading
echo $Test->property1// Get called for Test->$property1
?>


Useful if you want to proxy or lazy load properties yet want to have documentation and visibility in the code and debugging compared to __get(), __isset(), __set() on non-existent inaccessible properties.

[#4] cottton at i-stats dot net [2014-09-13 14:58:27]

Actually you dont need __set ect imo.  
You could use it to set (pre-defined) protected (and in "some" cases private) properties . But who wants that? 
(test it by uncommenting private or protected)
(pastebin because long ...) => http://pastebin.com/By4gHrt5

[#5] bimal at sanjaal dot com [2014-06-19 09:05:43]

Use of magic functions may make your private variables behave like public ones.
But it becomes strict; you cannot (in fact should not) assign any properties dynamically during the runtime.

<?php
class order
{
    private 
$OrderID='';
    private 
$OrderAmount=0.00;
    
    public function 
__set($name=''$value='')
    {
        if(
property_exists($this$name))
        {
            
$this->$name $value;
        }
    }

    public function 
__get($name='')
    {
        
$value null;
        if(
property_exists($this$name))
        {
            
$value $this->$name;
        }
        return 
$value;
    }
}

$order = new order();
$order->OrderID '201305062202';
$order->OrderAmount 23.45;
$order->InvalidMember 'Missed Assignment';

echo 
'<pre>'print_r($ordertrue), '</pre>';

?>


Outputs:
order Object
(
    [OrderID:order:private] => 201305062202
    [OrderAmount:order:private] => 23.45
)

[#6] ortreum [2014-06-12 09:31:46]

You can use __call and __callStatic for an very simple function based caching approach. If your method result needs to be cached based on its arguments then you only have to add cached_ infront of the name.

class thebase
{

private static $cache = array();

private static function argsAndCache($name, $args)
{
$hasArray = false;
$argsX = $cache = '';
if (count($args) > 0)
{
foreach ($args as $v) if ($hasArray = (is_array($v) || is_object($v))) break;
$argsX = '$args[' . implode('], $args[', array_keys($args)) . ']';
$cache = ($hasArray) 
? md5(json_encode($args)) 
: '[$args[' . implode(']], [$args[', array_keys($args)) . ']]';
}
$cache = 'self::$cache[\'' . $name . '\']' . $cache;
return array($argsX, $cache);
}

public function __call($name, $args)
{
list($argsX, $cache) = self::argsAndCache($name, $args);
eval('$result = isset(' . $cache . ') 
? ' . $cache . ' 
: $this->cached_' . $name . '(' . $argsX . ');');
return $result;
}

public static function __callStatic($name, $args)
{
list($argsX, $cache) = self::argsAndCache($name, $args);
eval('$result = isset(' . $cache . ') 
? ' . $cache . ' 
: ' . get_called_class() . '::cached_' . $name . '(' . $argsX . ');');
return $result;
}

}

class theChild extends thebase
{
public function cached_abc()
{
return 1;
}

public static function cached_s_abc()
{
return 1;
}

public static function normal()
{
return 1;
}

}

$x = new theChild();
echo $x->abc();
echo theChild::s_abc();
echo theChild::normal();

[#7] cavetroll3000 at gmail dot com [2014-05-24 13:40:56]

If you need to also use the magic setter in your constructor, you may use this example:

<?php
class person {
        private 
$name;
        private 
$addr;
        private 
$city;

        function 
__construct($n,$a,$c) {
                
$this->__set('name'$n); // Pushing "name" through the setter where it will be validated and sanitated.
                
$this->addr $a;         // Adress is not validated
              
$this->city $c;         // Neither is this...
//                $this->__set('city',$c);         // But this is...
        
}

        public function 
__get($property) {if (property_exists($this$property)) {return $this->$property;}}

        public function 
__set($property$value) {
                if (
property_exists($this$property)) {
                        echo 
"<pre>Setting $value</pre>";

                        
// ... Clever code for sanitation...
                        
if ($value == "Evil input from user"){
                                echo 
"<pre>Oh no! Bad user! Go sit in the corner!</pre>";
                                
$this->$property null;
                        } else {
                                
$this->$property $value;
                        }
                }
                return 
$this;
        }
}

$cathy = new person('Cathy','9 Dark and Twisty','Evil input from user');
$cathy->name "Kitty Kat";

echo 
"<pre>" print_r($cathy,1) . "</pre>";
echo 
"<pre>" $cathy->city "</pre>";
?>


This code will output:

Setting Kitty Kat
person Object
(
    [name:person:private] => Kitty Kat
    [addr:person:private] => 9 Dark and Twisty
    [city:person:private] => Evil input from user
)
Evil input from user

So it is important to push variables from the user through the setters even in the constructor.

[#8] N3hu3n [2014-03-29 17:43:15]

you can call the method _call passing parameters by reference:

<?php
class MagicCallReference()
{
public function 
__call($name$params)
 {
        
var_dump($params);
    
$params[0][0] = '222';
 }
}
$magicReference = new MagicCallReference();

$test 3;
var_dump($test);

$magicReference->setReference(array(&$info));

var_dump($test);
?>


Output:

int 3

array (size=1)
  0 => 
    array (size=1)
      0 => &int 3

string '222' (length=3)

[#9] Nehuen [2014-03-12 05:43:35]

Overloading other classical typed languages ??can be emulated in a simple way:

<?php
 class overload
 {
protected function overloaded($method, $params, $type = false, $instance = false)
{
$method .= count($params);
if($type && $params)
{
foreach($params as $param)
$method .= '_'.(gettype($param) == 'object' ? ($instance ? get_class($param) : 'object') : gettype($param));
}

if(method_exists($this, $method))
call_user_func_array(array($this,$method),$params);
}
 }

 class test_overload extends overload
 {
public function test_function() { $this->overloaded('test', func_get_args(), true); }

protected function test0()
{
echo('<br>Hello, I did not pass any parameters.');
}

protected function test1_string($string)
{
echo('<br>Hello, I spent this string: '.$string);
}

protected function test1_integer($integer)
{
echo('<br>Hello, I spent this integer: '.$integer);
}

protected function test2_integer_string($integer, $string)
{
echo('<br>Hello, I spent this integer: '.$integer.' and this string: '.$string);
}

protected function test2_string_integer($string, $integer)
{
echo('<br>Hello, I spent this string: '.$integer.' and this integer: '.$string);
}
 }
 
 $test = new test_overload();
 $test->test_function();
 $test->test_function('test');
 $test->test_function(5);
 $test->test_function(5, 'test');
 $test->test_function('test', 5);
 $test->test_function('2');

[#10] Nanhe Kumar [2014-01-23 10:28:14]

<?php
//How can implement __call function
class Student {

    protected 
$_name;
    protected 
$_email;
    

    public function 
__call($name$arguments) {
        
$action substr($name03);
        switch (
$action) {
            case 
'get':
                
$property '_' strtolower(substr($name3));
                if(
property_exists($this,$property)){
                    return 
$this->{$property};
                }else{
                    echo 
"Undefined Property";
                }
                break;
            case 
'set':
                
$property '_' strtolower(substr($name3));
                if(
property_exists($this,$property)){
                    
$this->{$property} = $arguments[0];
                }else{
                    echo 
"Undefined Property";
                }
                
                break;
            default :
                return 
FALSE;
        }
    }

}

$s = new Student();
$s->setNam('Nanhe Kumar');
$s->setEmail('nanhe.kumar@gmail.com');
echo 
$s->getName(); //Nanhe Kumar
echo $s->getEmail(); // nanhe.kumar@gmail.com
$s->setAge(10); //Undefined Property
?>

[#11] Nanhe Kumar [2014-01-17 22:47:30]

<?php
//How can implement __call function you understand better
class Employee {

    protected 
$_name;
    protected 
$_email;
    protected 
$_compony;

    public function 
__call($name$arguments) {
        
$action substr($name03);
        switch (
$action) {
            case 
'get':
                
$property '_' strtolower(substr($name3));
                if(
property_exists($this,$property)){
                    return 
$this->{$property};
                }else{
                    
$trace debug_backtrace();
                    
trigger_error('Undefined property  ' $name ' in ' $trace[0]['file'] . ' on line ' $trace[0]['line'], E_USER_NOTICE);
                    return 
null;
                }
                break;
            case 
'set':
                
$property '_' strtolower(substr($name3));
                if(
property_exists($this,$property)){
                    
$this->{$property} = $arguments[0];
                }else{
                    
$trace debug_backtrace();
                    
trigger_error('Undefined property  ' $name ' in ' $trace[0]['file'] . ' on line ' $trace[0]['line'], E_USER_NOTICE);
                    return 
null;
                }
                
                break;
            default :
                return 
FALSE;
        }
    }

}

$s = new Employee();
$s->setName('Nanhe Kumar');
$s->setEmail('nanhe.kumar@gmail.com');
echo 
$s->getName(); //Nanhe Kumar
echo $s->getEmail(); // nanhe.kumar@gmail.com
$s->setAge(10); //Notice: Undefined property setAge in
?>

[#12] theaceofthespade at gmail dot com [2012-03-23 21:35:55]

A word of warning!  It may seem obvious, but remember, when deciding whether to use __get, __set, and __call as a way to access the data in your class (as opposed to hard-coding getters and setters), keep in mind that this will prevent any sort of autocomplete, highlighting, or documentation that your ide mite do.

Furthermore, it beyond personal preference when working with other people.  Even without an ide, it can be much easier to go through and look at hardcoded member and method definitions in code, than having to sift through code and piece together the method/member names that are assembled in __get and __set.

If you still decide to use __get and __set for everything in your class, be sure to include detailed comments and documenting, so that the people you are working with (or the people who inherit the code from you at a later date) don't have to waste time interpreting your code just to be able to use it.

[#13] ari at asu dot edu [2011-10-31 01:57:59]

It may be important to note that when __set($name, $value) is called, $name gets entered into the symbol table for that call stack. This means that if a property is set within __set, it will only work if that property's name appears in the call stack (ie if it matches $name). If not, __set will be called again on the new property recursively.

<?php
class test
{
    function 
__set($name$value)
    {
        echo 
"__set($name$value);\n";
        
$this->prop3 $value// This will call __set(prop3, value)
        
$this->prop2 $value// This will call __set(prop2, value)
        
$this->prop1 $value// This will NOT call __set(prop1, value) because $name == prop1.
    
}
}

$test = new test;
$test->prop1 'value';
?>


It could be thought of like this: within the function __set($name, $value), $this->[property name] = [value] will recurse unless property name == $name. Within the call stack of __set, __set will never recurse on the same name twice. Once you leave __set (without actually creating the property), the call stack ends and all bets are off. 

What this means:
You cannot do things like setting an (uppercase) property to lowercase within __set without expecting __set to be called twice. If you were to add an underscore to the property name, you can expect an infinite recursion loop.

I hope someone finds this useful. It drove me nuts for a few hours.

[#14] dans at dansheps dot com [2011-08-01 08:38:52]

Since this was getting me for a little bit, I figure I better pipe in here...

For nested calls to private/protected variables(probably functions too) what it does is call a __get()  on the first object, and if you return the nested object, it then calls a __get() on the nested object because, well it is protected as well.

EG:
<?php
class A
{
protected 
$B

public function __construct()
{
$this->= new B();
}

public function 
__get($variable)
{
echo 
"Class A::Variable " $variable "\n\r";
$retval $this->{$variable};
return 
$retval;
}
}

class 
B
{
protected 
$val

public function __construct()
{
$this->val 1;
}

public function 
__get($variable)
{
echo 
"Class B::Variable " $variable "\n\r";
$retval $this->{$variable};
return 
$retval;
}
}

$A = new A();

echo 
"Final Value: " $A->B->val;
?>


That will return something like...

Class A::Variable B
Class B::Variable val
Final Value: 1

It seperates the calls into $A->B and $B->val

Hope this helps someone

[#15] jk at jankriedner dot de [2011-06-07 03:15:54]

You should take care when using properties retrieved via __get() in functions that expect arguments to be passed by reference (e.g. mysqli_stmt_bind_param). The reference is NOT set to the property itself, but to the value returned by __get().
Thus, binding a property retrieved via __get() to a statement will let the statement be executed always with the value the property had when calling bind_param, not with the current value it has when calling execute().
E.g.:
<?php
error_reporting
(E_ALL);
class 
foo {
    protected 
$bar;
    
    public function 
__construct() {
        
$this->bar "Hello World!";
    }
    public static function 
factory() {
        return new 
self;
    }
    public function 
__get($property) {
        if(!
property_exists($this,$property)) {
            throw new 
InvalidArgumentException("Property {$property} doesn't exist");
        }
        return 
$this->$property;
    }
    public function 
setBar($value) {
        
$this->bar $value;
    }
}
$foo = new foo();
echo 
$foo->bar;    // Ouputs: Hello World!
$db = new mysqli("localhost","root","","tests");
$sql "INSERT INTO foo SET bar=?";
$res $db->prepare($sql);
$res->bind_param("s",$foo->bar);  // Notice: Indirect modification of overloaded property foo::$bar has no effect in /var/www/overload.php on line 24
$res->execute();  // Writes "Hello World!" to database
$foo->setBar("Goodbye");
echo 
$foo->bar;   // Outputs: Goodbye
$res->execute();  // Writes "Hello World!" to database
?>

[#16] Daniel Smith [2011-05-23 16:15:46]

Be careful of __call in case you have a protected/private method. Doing this:

<?php
class TestMagicCallMethod {
    public function 
foo()
    {
        echo 
__METHOD__.PHP_EOL;
    }

    public function 
__call($method$args)
    {
        echo 
__METHOD__.PHP_EOL;
        if(
method_exists($this$method))
        {
            
$this->$method();
        }
    }
    
    protected function 
bar()
    {
        echo 
__METHOD__.PHP_EOL;
    }

    private function 
baz()
    {
        echo 
__METHOD__.PHP_EOL;
    }
}

$test    =    new TestMagicCallMethod();
$test->foo();


$test->bar();


$test->baz();

?>


..is probably not what you should be doing. Always make sure that the methods you call in __call are allowed as you probably dont want all the private/protected methods to be accessed by a typo or something.

[#17] jan dot machala at email dot cz [2011-04-15 03:47:32]

Example of usage __call() to have implicit getters and setters

<?php
class Entity {
    public function 
__call($methodName$args) {
        if (
preg_match('~^(set|get)([A-Z])(.*)$~'$methodName$matches)) {
            
$property strtolower($matches[2]) . $matches[3];
            if (!
property_exists($this$property)) {
                throw new 
MemberAccessException('Property ' $property ' not exists');
            }
            switch(
$matches[1]) {
                case 
'set':
                    
$this->checkArguments($args11$methodName);
                    return 
$this->set($property$args[0]);
                case 
'get':
                    
$this->checkArguments($args00$methodName);
                    return 
$this->get($property);
                case 
'default':
                    throw new 
MemberAccessException('Method ' $methodName ' not exists');
            }
        }
    }

    public function 
get($property) {
        return 
$this->$property;
    }

    public function 
set($property$value) {
        
$this->$property $value;
        return 
$this;
    }

    protected function 
checkArguments(array $args$min$max$methodName) {
        
$argc count($args);
        if (
$argc $min || $argc $max) {
            throw new 
MemberAccessException('Method ' $methodName ' needs minimaly ' $min ' and maximaly ' $max ' arguments. ' $argc ' arguments given.');
        }
    }
}

class 
MemberAccessException extends Exception{}

class 
Foo extends Entity {
    protected 
$a;
}

$foo = new Foo();
$foo->setA('some'); // outputs some
echo $foo->getA();

class 
Bar extends Entity {
    protected 
$a;
    

    
public function setA($a) {
        if (!
preg_match('~^[0-9a-z]+$~i'$a)) {
            throw new 
MemberAccessException('A can be only alphanumerical');
        }
        
$this->$a;
        return 
$this;
    }
}

$bar = new Bar();
$bar->setA('abc123'); // ok
$bar->setA('[]
    
public function suggest_alternative($property)
    {
        
$parts explode('_',$property);
        foreach(
$parts as $i => $p) if ($p == '_' || $p == 'id') unset($parts[$i]);

        echo 
'checking for <b>'.implode(', ',$parts)."</b> and suggesting the following:<br/>\n";

        echo 
"<ul>";
        foreach(
$this as $key => $value)
            foreach(
$parts as $p)
                if (
stripos($key$p) !== false) print '<li>'.$key."</li>\n";
        echo 
"</ul>";
    }

just put it in your __get() or __set() like so:

    public function 
__get($property)
    {
            echo 
"<p><font color='#ff0000'>Attempted to __get() non-existant property/variable '".$property."' in class '".$this->get_class_name()."'.</font><p>\n";
            
$this->suggest_alternative($property);
            exit;
    }
?>

[#28] niehztog [2009-02-14 10:59:15]

If you got a parent class agregating(not inheriting) a number of child classes in an array, you can use the following to allow calling methods of the parent object on agregated child objects:
<?php
class child {
    public 
$holder null;

    public function 
__call($name$arguments) {
        if(
$this->holder instanceof parentClass && method_exists($this->holder$name)) {
            return 
call_user_func_array(array($this->holder$name), $arguments);
        }
        else {
            
trigger_error();
        }
    }
}

class 
parentClass {
    private 
$children = array();

    function 
__construct() {
        
$this->children[0] = new child();
        
$this->children[0]->holder $this;
    }

    function 
getChild($number) {
        if(!isset(
$this->children[$number])) {
            return 
false;
        }
        return 
$this->children[$number];
    }

    function 
test() {
        return 
'it works';
    }
}

$parent = new parentClass();
$firstChild $parent->getChild(0);
echo 
$firstChild->test(); //should output 'it works'
?>

[#29] jorge dot hebrard at gmail dot com [2008-12-23 16:55:47]

This is a great way to give different permissions to parent classes.

<?php
class A{
    private 
$b;
    function 
foo(){
        
$this->= new B;
        echo 
$this->b->protvar;
    }
}
class 
extends A{
    protected 
$protvar="protected var";
    public function 
__get($nm) {
       echo 
"Default $nm value";
    }
}
$a = new A;
$b = new B;
$a->foo(); // prints "protected var"
echo $b->protvar// prints "Default protvar value"
?>

This way, you can help parent classes to have more power with protected members.

[#30] Ant P. [2008-12-21 08:40:59]

Be extra careful when using __call():  if you typo a function call somewhere it won't trigger an undefined function error, but get passed to __call() instead, possibly causing all sorts of bizarre side effects.
In versions before 5.3 without __callStatic, static calls to nonexistent functions also fall through to __call!
This caused me hours of confusion, hopefully this comment will save someone else from the same.

[#31] erick2711 at gmail dot com [2008-11-14 07:33:54]

<?php

class menu extends nodoMenu{
    private 
$cssc = array();
    
    public function 
__toString(){  //Just to show, replace with something better.
        
$stringMenu "<pre>\n";$stringMenu .= $this->strPrint();$stringMenu .= "</pre>\n";
        return 
$stringMenu;
    }

    public function 
__construct($cssn null){
        
parent::__construct();
        if (isset(
$cssn) && is_array($cssn)){$this->cssc $cssn;}
        
$this->buildMenu();
    }
    
    public function 
buildMenu(){
        
$this->add('server',
                   
'Server',
                   
'server.php');
            
$this->server->add('personalD',
                               
'Personal Data',
                               
'server/personal.php');
            
$this->server->add('personalI',
                               
'Personal Interviews',
                               
'server/personalI.php');
                
$this->server->personalI->add('detailsByIer',
                                              
'Detalis by Interviewer',
                                              
'server/personalI.php?tab=detailsByIer');
        
//(...)
        
return $this;
    }
}

//Testing
$meuMenu = new menu;
echo 
$meuMenu;

?>

[#32] erick2711 at gmail dot com [2008-11-14 07:09:53]

<?php

class nodoMenu{

    protected 
$p  = array();
    protected 
$c = array();

    public function 
__construct($t ''$uri ''$css null$n 0$i=0){
        
$this->p['t'] = $t;$this->p['uri'] = $uri;$this->p['css'] = $css;$this->p['n'] = $n;$this->p['i'] = $i;$this->p['q'] = 0;return $this;
    }

    public function 
add($cn$ct ''$cl ''$css null){
        
$nc = new nodoMenu($ct$cl$css$this->p['n'] + 1$this->p['q']);$this->c[$cn] = $nc;$this->p['q'] += 1;return $this->c[$cn];
    }

    private function 
isParameter($pn){
        return 
array_key_exists($pn$this->p);
    }
    
    public function 
__isset($pn){
        if (
$this->isParameter($pn)){return(!is_null($this->p[$pn]));}
        else{return(
array_key_exists($pn$this->c));}
    }

    public function 
remove($cn){
        if (
array_key_exists($cn$this->c)){$this->p['q'] -= 1;unset($this->c[$cn]);}
    }

    public function 
__unset($pn){
        if (
$this->isParameter($pn)){$this->p[$pn] = null;}
        else{
$this->remove($pn);}
    }

    public function 
__set($pn$v){
        
$r null;
        if (
$this->isParameter($pn)){$this->p[$pn] = $v;$r $v;}
        else{if (
array_key_exists($pn$this->c)){$this->c[$pn] = $v;$r $this->c[$pn];}
            else{
$r $this->add($pn);}}    
        return 
$r;
    }        

    public function 
__get($pn){
        
$v null;
        if (
$this->isParameter($pn)){$v $this->p[$pn];}
        else{if (
array_key_exists($pn$this->c)){$v $this->c[$pn];}
            else{
$v $this->add($pn);}}
        return 
$v;
    }
    
    public function 
hasChilds(){
        return(isset(
$this->c[0]));
    }
    
    public function 
child($i){
        return 
$this->c[$i];
    }
    
    public function 
strPrint($bm ''){   //Just to show, replace with something better.
        
$m '';$r '';$n $this->p['n'];
        if (
$n 0){switch($n){case 0:case 1$qs 0; break;case 2$qs 2; break;case 3$qs 6; break;case 4$qs 12; break;case 5$qs 20; break;case 6$qs 30; break;case 7$qs 42; break;case 8$qs 56; break;}
            
$tab str_repeat('&nbsp;'$qs);$r .= $tab;
            if (
$bm <> ''){$m $bm.'.';}
            
$im $this->p['i'] + 1;$m .= $im;$r .= $m.' ';$r .= $this->p['t']."<br>\n";
        }
        foreach (
$this->as $child){$r .= $child->strPrint($m);}
        return 
$r;
    }
    
    public function 
__toString(){
        return 
$this->strPrint();
    }
}
?>

[#33] strafvollzugsbeamter at gmx dot de [2008-07-16 12:57:09]

The following works on my installation (5.2.6 / Windows):
<?php
class G
{
    private 
$_p = array();
    
    public function 
__isset($k)
    {
        return isset(
$this->_p[$k]);
    }
        
    public function 
__get($k)
    {
        
$v NULL;
        if (
array_key_exists($k$this->_p))
        {
            
$v $this->_p[$k];
        }
        else
        {
            
$v $this->{$k} = $this;
        }
        
        return 
$v;
    }
    
    public function 
__set($k$v)
    {
        
$this->_p[$k] = $v;
        
        return 
$this;
    }    
}

$s = new G();
$s->A->B->'FOO';
$s->X->Y->= array ('BAR');

if (isset(
$s->A->B->C))
{
    print(
$s->A->B->C);
}
else
{
    print(
'A->B->C is NOT set');
}

if (isset(
$s->X->Y->Z))
{
    
print_r($s->X->Y->Z);
}
else
{
    print(
'X->Y->Z is NOT set');
}

// prints: FOOArray ( [0] => BAR )
?>


... have fun and  ...

[#34] Anonymous [2008-04-30 13:02:54]

This is a generic implementation to use getter, setter, issetter and unsetter for your own classes.

<?php
abstract class properties
{
  public function 
__get$property )
  {
    if( ! 
is_callable( array($this,'get_'.(string)$property) ) )
      throw new 
BadPropertyException($this, (string)$property);

    return 
call_user_func( array($this,'get_'.(string)$property) );
  }

  public function 
__set$property$value )
  {
    if( ! 
is_callable( array($this,'set_'.(string)$property) ) )
      throw new 
BadPropertyException($this, (string)$property);

    
call_user_func( array($this,'set_'.(string)$property), $value );
  }
  
  public function 
__isset$property )
  {
    if( ! 
is_callable( array($this,'isset_'.(string)$property) ) )
      throw new 
BadPropertyException($this, (string)$property);

    return 
call_user_func( array($this,'isset_'.(string)$property) );
  }

  public function 
__unset$property )
  {
    if( ! 
is_callable( array($this,'unset_'.(string)$property) ) )
      throw new 
BadPropertyException($this, (string)$property);

    
call_user_func( array($this,'unset_'.(string)$property) );
  }
}
?>

[#35] nospam michael AT netkey DOT at nospam [2008-04-01 10:10:53]

you CAN write into ARRAYS by using __set and __get magic functions. 

as has been mentioned before $obj->var['key'] = 'test'; does call the __get method of $obj, and there is no way to find out, if the method has been called for setting purposes.

the solution is quite simple: use __get to return the array by reference. then you can write into it:

<?php
class setter{
  private 
$_arr = array();

  public function 
__set($name$value){
    
$this->_arr[$name] = $value;
  }

  public function &
__get($name){
    if (isset(
$this->_arr[$name])){
      return 
$this->_arr[$name];
    } else return 
null;
  }

}

?>

[#36] timshaw at mail dot NOSPAMusa dot com [2008-01-28 21:47:10]

The __get overload method will be called on a declared public member of an object if that member has been unset.

<?php
class {
  public 
$p ;
  public function 
__get($name) { return "__get of $name; }
}

$c = new ;
echo 
$c->p"\n" ;    // declared public member value is empty
$c->;
echo 
$c->p"\n" ;    // declared public member value is 5
unset($c->p) ;
echo 
$c->p"\n" ;    // after unset, value is "__get of p"
?>

[#37] jj dhoT maturana aht gmail dhot com [2008-01-25 04:16:05]

There isn't some way to overload a method when it's called as a reflection method:

<?php

class TestClass {
  function 
__call($method$args) {
    echo 
"Method {$method} called with args: " print_r($argsTRUE);
  }
}

$class = new ReflectionClass("TestClass");
$method $class->getMethod("myMehtod");

//Fatal error:  Uncaught exception 'ReflectionException' with message 'Method myMethod' does not exist'

?>


Juan.

[#38] v dot umang at gmail dot com [2008-01-11 21:20:13]

If you want to be able to overload a variable from within a class and this is your code:
<?php
class myClass
{
    private 
$data;
    public function 
__set($var$val)
    {
        
$this->data[$var] = $val;
    }
    public function 
__get($var)
    {
        
$this->data[$var] = $val;
    }
}
?>


There is a problem if you want to call these variables from within the class, as you you want to access data['data'] then you can't say $this->data as it will return the array $data. Therefore a simple solution is to name the array $_data. So in your __get and __set you will say $this->_data ... rather than $this->data. I.E:
<?php
class myClass
{
    private 
$_data;
    public function 
__set($var$val)
    {
        
$this->_data[$var] = $val;
    }
    public function 
__get($var)
    {
        
$this->_data[$var] = $val;
    }
}
?>


Umang

[#39] Anonymous [2008-01-09 07:45:50]

It should be noted that __call will trigger only for method calls on an instantiated object, and cannot be used to 'overload' static methods.  For example:

<?php

class TestClass {
  function 
__call($method$args) {
    echo 
"Method {$method} called with args: " print_r($argsTRUE);
  }
}

// this will succeed
$obj = new TestClass();
$obj->method_doesnt_exist();

// this will not
TestClass::method_doesnt_exist();

?>


It would be useful if the PHP devs would include this in a future release, but in the meantime, just be aware of that pitfall.

[#40] egingell at sisna dot com [2007-12-20 19:54:52]

The PHP devs aren't going to implement true overloading because: PHP is not strictly typed by any stretch of the imagination (0, "0", null, false, and "" are the same, for example) and unlike Java and C++, you can pass as many values as you want to a function. The extras are ignored unless you fetch them using func_get_arg(int) or func_get_args(), which is often how I "overload" a function/method, and fewer than the declared number of arguments will generate an E_WARNING, which can be suppressed by putting '@' before the function call, but the function will still run as if you had passed null where a value was expected.

<?php
class someClass {
    function 
whatever() {
        
$args func_get_args();
    
        
// public boolean whatever(boolean arg1) in Java
        
if (is_bool($args[0])) {
            
// whatever(true);
            
return $args[0];
    
        
// public int whatever(int arg1, boolean arg2) in Java
        
} elseif(is_int($args[0]) && is_bool($args[1])) {
            
// whatever(1, false)
            
return $args[0];
    
        } else {
            
// public void whatever() in Java
            
echo 'Usage: whatever([int], boolean)';
        }
    }
}
?>


// The Java version:
public class someClass {
public boolean whatever(boolean arg1) {
    return arg1;
}

public int whatever(int arg1, boolean arg2) {
    return arg1;
}

public void whatever() {
    System.out.println("Usage: whatever([int], boolean)");
}
}

[#41] matthijs at yourmediafactory dot com [2007-12-16 11:09:20]

While PHP does not support true overloading natively, I have to disagree with those that state this can't be achieved trough __call. 

Yes, it's not pretty but it is definately possible to overload a member based on the type of its argument. An example:
<?php 
class 
   
  public function 
__call ($member$arguments) { 
    if(
is_object($arguments[0])) 
      
$member $member 'Object'
    if(
is_array($arguments[0])) 
      
$member $member 'Array'
    
$this -> $member($arguments); 
  } 
   
  private function 
testArray () { 
    echo 
"Array."
  } 
   
  private function 
testObject () { 
    echo 
"Object."
  } 


class 



$class = new A
$class -> test(array()); // echo's 'Array.' 
$class -> test(new B); // echo's 'Object.' 
?>


Of course, the use of this is questionable (I have never needed it myself, but then again, I only have a very minimalistic C++ & JAVA background). However, using this general principle and optionally building forth on other suggestions a 'form' of overloading is definately possible, provided you have some strict naming conventions in your functions. 

It would of course become a LOT easier once PHP'd let you declare the same member several times but with different arguments, since if you combine that with the reflection class 'real' overloading comes into the grasp of a good OO programmer. Lets keep our fingers crossed!

[#42] php_is_painful at world dot real [2007-10-19 07:49:21]

This is a misuse of the term overloading. This article should call this technique "interpreter hooks".

[#43] egingell at sisna dot com [2007-09-15 05:12:49]

Small vocabulary note: This is *not* "overloading", this is "overriding".

Overloading: Declaring a function multiple times with a different set of parameters like this:
<?php

function foo($a) {
    return 
$a;
}

function 
foo($a$b) {
    return 
$a $b;
}

echo 
foo(5); // Prints "5"
echo foo(52); // Prints "7"

?>


Overriding: Replacing the parent class's method(s) with a new method by redeclaring it like this:
<?php

class foo {
    function new(
$args) {
        
// Do something.
    
}
}

class 
bar extends foo {
    function new(
$args) {
        
// Do something different.
    
}
}

?>

[#44] ac221 at sussex dot ac dot uk [2007-09-01 10:07:04]

Just Noting the interesting behavior of __set __get , when modifying objects contained in overloaded properties.

<?php
class foo {
    public 
$propObj;
    public function 
__construct(){
        
$propObj = new stdClass();
    }
    public function 
__get($prop){
        echo(
"I'm Being Got ! \n"); 
        return 
$this->propObj->$prop;
    }
    public function 
__set($prop,$val){
        echo(
"I'm Being Set ! \n");
        
$this->propObj->$prop $val;
    }
}
$test = new foo();
$test->barProp = new stdClass(); // I should invoke set
$test->barProp->barSubProp 'As Should I';
$test->barProp->barSubProp 'As Should I';
$test->barProp = new stdClass(); // As should i
?>


Outputs:

I'm Being Set !
I'm Being Got !
I'm Being Got !
I'm Being Set !

Whats happening, is PHP is acquiring a reference to the object, triggering __get; Then applying the changes to the object via the reference.

Which is the correct behaviour; objects being special creatures, with an aversion to being cloned...

Unfortunately this will never invoke the __set handler, even though it is modifying a property within 'foo', which is slightly annoying if you wanted to keep track of changes to an objects overloaded properties.

I guess Journaled Objects will have to wait till PHP 6 :)

[#45] php at sleep is the enemy dot co dot uk [2007-07-23 07:23:43]

Just to reinforce and elaborate on what DevilDude at darkmaker dot com said way down there on 22-Sep-2004 07:57.

The recursion detection feature can prove especially perilous when using __set. When PHP comes across a statement that would usually call __set but would lead to recursion, rather than firing off a warning or simply not executing the statement it will act as though there is no __set method defined at all. The default behaviour in this instance is to dynamically add the specified property to the object thus breaking the desired functionality of all further calls to __set or __get for that property.

Example:

<?php

class TestClass{

    public 
$values = array();
    
    public function 
__get($name){
        return 
$this->values[$name];
    }
    
    public function 
__set($name$value){
        
$this->values[$name] = $value;
        
$this->validate($name);
    }

    public function 
validate($name){
        

        
$this->$name trim($this->$name);
    }
}

$tc = new TestClass();

$tc->foo 'bar';
$tc->values['foo'] = 'boing';

echo 
'$tc->foo == ' $tc->foo '<br>';
echo 
'$tc ' . (property_exists($tc'foo') ? 'now has' 'still does not have') . ' a property called "foo"<br>';



?>

[#46] Adeel Khan [2007-07-10 13:18:39]

Observe:

<?php
class Foo {
    function 
__call($m$a) {
        die(
$m);
    }
}

$foo = new Foo;
print 
$foo->{'wow!'}();

// outputs 'wow!'
?>


This method allows you to call functions with invalid characters.

[#47] alexandre at nospam dot gaigalas dot net [2007-07-07 22:59:35]

PHP 5.2.1

Its possible to call magic methods with invalid names using variable method/property names:

<?php

class foo
{
    function 
__get($n)
    {
        
print_r($n);
    }
    function 
__call($m$a)
    {
        
print_r($m);
    }
}

$test = new foo;
$varname 'invalid,variable+name';
$test->$varname;
$test->$varname();

?>


I just don't know if it is a bug or a feature :)

[#48] MagicalTux at ooKoo dot org [2006-09-06 02:35:29]

Since many here probably wanted to do ?real? overloading without having to think too much, here's a generic __call() function for those cases.

Little example :
<?php
class OverloadedClass {
        public function 
__call($f$p) {
                if (
method_exists($this$f.sizeof($p))) return call_user_func_array(array($this$f.sizeof($p)), $p);
                
// function does not exists~
                
throw new Exception('Tried to call unknown method '.get_class($this).'::'.$f);
        }

        function 
Param2($a$b) {
                echo 
"Param2($a,$b)\n";
        }

        function 
Param3($a$b$c) {
                echo 
"Param3($a,$b,$c)\n";
        }
}

$o = new OverloadedClass();
$o->Param(4,5);
$o->Param(4,5,6);
$o->ParamX(4,5,6,7);
?>


Will output :
Param2(4,5)
Param3(4,5,6)

Fatal error: Uncaught exception 'Exception' with message 'Tried to call unknown method OverloadedClass::ParamX' in overload.php:7
Stack trace:
#0 [internal function]: OverloadedClass->__call('ParamX', Array)
#1 overload.php(22): OverloadedClass->ParamX(4, 5, 6, 7)
#2 {main}
  thrown in overload.php on line 7

[#49] jstubbs at work-at dot co dot jp [2006-09-02 09:12:49]

<?php $myclass->foo['bar'] = 'baz'?>
 
When overriding __get and __set, the above code can work (as expected) but it depends on your __get implementation rather than your __set. In fact, __set is never called with the above code. It appears that PHP (at least as of 5.1) uses a reference to whatever was returned by __get. To be more verbose, the above code is essentially identical to:
 
<?php
$tmp_array 
= &$myclass->foo;
$tmp_array['bar'] = 'baz';
unset(
$tmp_array);
?>


Therefore, the above won't do anything if your __get implementation resembles this:

<?php 
function __get($name) {
    return 
array_key_exists($name$this->values)
        ? 
$this->values[$name] : null;
}
?>


You will actually need to set the value in __get and return that, as in the following code:

<?php
function __get($name) {
    if (!
array_key_exists($name$this->values))
        
$this->values[$name] = null;
    return 
$this->values[$name];
}
?>

[#50] mnaul at nonsences dot angelo dot edu [2006-07-11 12:58:46]

This is just my contribution. It based off of many diffrent suggestions I've see thought the manual postings.
It should fit into any class and create default get and set methods for all you member variables. Hopfuly its usefull.
<?php
public function __call($name,$params)
{
if( preg_match('/(set|get)(_)?/',$name) )
{
if(substr($name,0,3)=="set")
{
$name = preg_replace('/set(_)?/','',$name);
if(property_exists(__class__,$name))
{
$this->{$name}=array_pop($params);
return true;
}
else
{
//call to class error handler
return false;
}
return true;
}
elseif(substr($name,0,3)=="get")
{
$name = preg_replace('/get(_)?/','',$name);
if(property_exists(__class__,$name) )
{
return $this->{$name};
}
else
{
//call to class error handler
return false;
}
}
else
{
//call to class error handler
return false;
}
}
else
{
die("method $name dose not exist\n");
}
return false;
}

[#51] Sleepless [2006-02-23 23:22:40]

Yet another way of providing support for read-only properties.  Any property that has
"pri_" as a prefix will NOT be returned, period, any other property will be returned 
and if it was declared to be "protected" or "private" it will be read-only. (scope dependent of course)

<?php
function __get($var){
        if (
property_exists($this,$var) && (strpos($var,"pri_") !== 0) )
            return 
$this->{$var};
        else
            
//Do something
}
?>

[#52] derek-php at seysol dot com [2006-02-10 12:08:32]

Please note that PHP5 currently doesn't support __call return-by-reference (see PHP Bug #30959).

Example Code:

<?php

    
class test {
        public function &
__call($method$params) {

            
// Return a reference to var2
            
return $GLOBALS['var2'];
        }
        public function &
actual() {

            
// Return a reference to var1
            
return $GLOBALS['var1'];
        }
    }

    
$obj = new test;
    
$GLOBALS['var1'] = 0;
    
$GLOBALS['var2'] = 0;

    
$ref1 =& $obj->actual();
    
$GLOBALS['var1']++;

    echo 
"Actual function returns: $ref1 which should be equal to " $GLOBALS['var1'] . "<br/>\n";

    
$ref2 =& $obj->overloaded();
    
$GLOBALS['var2']++;

    echo 
"Overloaded function returns: $ref2 which should be equal to " $GLOBALS['var2'] . "<br/>\n";

?>

[#53] PHP at jyopp dotKomm [2005-12-22 11:01:45]

Here's a useful class for logging function calls.  It stores a sequence of calls and arguments which can then be applied to objects later.  This can be used to script common sequences of operations, or to make "pluggable" operation sequences in header files that can be replayed on objects later.

If it is instantiated with an object to shadow, it behaves as a mediator and executes the calls on this object as they come in, passing back the values from the execution.

This is a very general implementation; it should be changed if error codes or exceptions need to be handled during the Replay process.
<?php
class MethodCallLog {
    private 
$callLog = array();
    private 
$object;
    
    public function 
__construct($object null) {
        
$this->object $object;
    }
    public function 
__call($m$a) {
        
$this->callLog[] = array($m$a);
        if (
$this->object) return call_user_func_array(array(&$this->object,$m),$a);
        return 
true;
    }
    public function 
Replay(&$object) {
        foreach (
$this->callLog as $c) {
            
call_user_func_array(array(&$object,$c[0]), $c[1]);
        }
    }
    public function 
GetEntries() {
        
$rVal = array();
        foreach (
$this->callLog as $c) {
            
$rVal[] = "$c[0](".implode(', '$c[1]).");";
        }
        return 
$rVal;
    }
    public function 
Clear() {
        
$this->callLog = array();
    }
}

$log = new MethodCallLog();
$log->Method1();
$log->Method2("Value");
$log->Method1($a$b$c);
// Execute these method calls on a set of objects...
foreach ($array as $o$log->Replay($o);
?>

[#54] trash80 at gmail dot com [2005-12-03 20:59:31]

Problem: $a->b->c(); when b is not instantiated.
Answer: __get()

<?php

class a
{
   function 
__get($v)
   {
       
$this->$v = new $v;
       return 
$this->$v;
   }
}

class 
b
{
    function 
say($word){
        echo 
$word;
    }
}
$a = new a();
$a->b->say('hello world');

// echos 'hello world'
?>

[#55] seufert at gmail dot com [2005-11-01 16:25:50]

This allows you to seeminly dynamically overload objects using plugins.

<?php

class standardModule{}

class 
standardPlugModule extends standardModule
{
  static 
$plugptrs;
  public 
$var;
  static function 
plugAdd($name$mode$ptr)
  {
    
standardPlugModule::$plugptrs[$name] = $ptr;
  }
  function 
__call($fname$fargs)
  { print 
"You called __call($fname)\n";
    
$func standardPlugModule::$plugptrs[$fname];
    
$r call_user_func_array($funcarray_merge(array($this),$fargs));
    print 
"Done: __call($fname)\n";
    return 
$r;
  }
  function 
dumpplugptrs() {var_dump(standardPlugModule::$plugptrs); }
}

class 
extends standardPlugModule
{ function text() { return "Text"; } }
//Class P contained within a seperate file thats included
class p
{ static function plugin1($mthis$r)
  { print 
"You called p::plugin1\n";
    
print_r($mthis);
    
print_r($r);
  }
a::plugAdd('callme'0, array('p','plugin1'));

//Class P contained within a seperate file thats included
class p2
{ static function plugin2($mthis$r)
  { print 
"You called p2::plugin2\n";
    
$mthis->callme($r);
  }
a::plugAdd('callme2'0, array('p2','plugin2'));

$t = new a();
$testr = array(1,4,9,16);
print 
$t->text()."\n";
$t->callme2($testr);
//$t->dumpplugptrs();

?>


Will result in:
----------
Text
You called __call(callme2)
You called p2::plugin2
You called __call(callme)
You called p::plugin1
a Object
(
    [var] => 
)
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
)
Done: __call(callme)
Done: __call(callme2)
----------

This also clears up a fact that you can nest __call() functions, you could use this to get around the limits to __get() not being able to be called recursively.

[#56] [2005-08-26 07:32:20]

To those who wish for "real" overloading: there's not really any advantage to using __call() for this -- it's easy enough with func_get_args(). For example:

<?php

    
class Test
    
{
        
        public function 
Blah()
        {
            
            
$args func_get_args();
            
            switch (
count($args))
            {
                case 
1 break;
                case 
2 break;
            }
            
        }
        
    }

?>

[#57] NOTE: getter cannot call getter [2005-08-04 12:37:28]

By Design (http://bugs.php.net/bug.php?id=33998) you cannot call a getter from a getter or any function triggered by a getter:

<?php
class test
{
    protected 
$_a 6;

    function 
__get($key) {
        if(
$key == 'stuff') {
            return 
$this->stuff();
        } else if(
$key == 'a') {
            return 
$this->_a;
        }
    }

    function 
stuff()
    {
        return array(
'random' => 'key''using_getter' => 10 $this->a);
    }
}

$test = new test();
print 
'this should be 60: '.$test->stuff['using_getter'].'<br/>';       // prints "this should be 60: 0"
// [[ Undefined property:  test::$a ]] on /var/www/html/test.php logged.
print 'this should be 6: '.$test->a.'<br/>';                            // prints "this should be 6: 6"
?>

[#58] [2005-05-06 03:50:04]

Please note that PHP5's overloading behaviour is not compatible at all with PHP4's overloading behaviour.

[#59] Marius [2005-05-02 02:15:36]

for anyone who's thinking about traversing some variable tree
by using __get() and __set(). i tried to do this and found one
problem: you can handle couple of __get() in a row by returning
an object which can handle consequential __get(), but you can't
handle __get() and __set() that way.
i.e. if you want to:
<?php
    
print($obj->val1->val2->val3); // three __get() calls
?>
 - this will work,
but if you want to:
<?php
    $obj
->val1->val2 $val// one __get() and one __set() call
?>
 - this will fail with message:
"Fatal error: Cannot access undefined property for object with
 overloaded property access"
however if you don't mix __get() and __set() in one expression,
it will work:
<?php
    $obj
->val1 $val// only one __set() call
    
$val2 $obj->val1->val2// two __get() calls
    
$val2->val3 $val// one __set() call
?>


as you can see you can split __get() and __set() parts of
expression into two expressions to make it work.

by the way, this seems like a bug to me, will have to report it.

[#60] ryo at shadowlair dot info [2005-03-22 10:22:50]

Keep in mind that when your class has a __call() function, it will be used when PHP calls some other magic functions. This can lead to unexpected errors:

<?php
class TestClass {
    public 
$someVar;

    public function 
__call($name$args) {
        
// handle the overloaded functions we know...
        // [...]

        // raise an error if the function is unknown, just like PHP would
        
trigger_error(sprintf('Call to undefined function: %s::%s().'get_class($this), $name), E_USER_ERROR);
    }
}

$obj = new TestClass();
$obj->someVar 'some value';

echo 
$obj//Fatal error: Call to undefined function: TestClass::__tostring().
$serializedObj serialize($obj); // Fatal error: Call to undefined function: TestClass::__sleep().
$unserializedObj unserialize($someSerializedTestClassObject); // Fatal error: Call to undefined function: TestClass::__wakeup().
?>

[#61] thisisroot at gmail dot com [2005-02-18 07:27:45]

You can't mix offsetSet() of the ArrayAccess interface (http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html) and __get() in the same line.

Below, "FileManagerPrefs" is an object of class UserData which implements ArrayAccess. There's a protected array of UserData objects in the User class, which are returned from __get().
<?php
// This produces an error...
Application::getInstance()->user->FileManagerPrefs'base'] = 'uploads/jack';
?>


Creates this error:
Fatal error: Cannot access undefined property for object with overloaded property access in __FILE__ on line __LINE__

However, __get() and offsetGet() play deceptively well together.

<?php
// This works fine!
echo Application::getInstance()->user->FileManager['base'];
?>


I guess it's a dereferencing issue with __get(). In my case, it makes more sense to have a middle step (user->data['FileManager']['base']), but I wanted to tip off the community before I move on.

[#62] mileskeaton at gmail dot com [2004-12-23 12:23:50]

<?php

## THE PROBLEM:  Class with lots of attributes.  
## You want to use $o->getVarName() or $o->get_varname() style getters
## Some attributes have custom get functions, but the rest don't

## THE SOLUTION:  __call

class Person
    
{
    
## this top stuff is just an example.  could be anything.
    
private $name;
    private 
$age;
    private 
$weight;
    function 
__construct($name$age$weight)
        {
        
$this->name $name;
        
$this->age $age;
        
$this->weight $weight;
        }

    
##     PORTABLE: use this __call function in any class
    
function __call($val$x)
        {
        
# see if they're calling a getter method - and try to guess the variable requested
        
if(substr($val04) == 'get_')
            {
            
$varname substr($val4);
            }
        elseif(
substr($val03) == 'get')
            {
            
$varname substr($val3);
            }
        else
            {
            die(
"method $val does not exist\n");
            }
        
# now see if that variable exists:
        
foreach($this as $class_var=>$class_var_value)
            {
            if(
strtolower($class_var) == strtolower($varname))
                {
                return 
$class_var_value;
                }
            }
        return 
false;
        }

    
# IMPORTANT: you can keep some things private - or treat
    # some vars differently by giving them their own getter method
    # See how this function lies about Person's weight.
    
function getWeight()
        {
        return 
intval($this->weight .8);
        }
    }

$a = new Person('Miles'35200);

# these all work (case-insensitive):
print $a->get_name() . "\n";
print 
$a->getName() . "\n";
print 
$a->get_Name() . "\n";
print 
$a->getname() . "\n";

print 
$a->get_age() . "\n";
print 
$a->getAge() . "\n";
print 
$a->getage() . "\n";
print 
$a->get_Age() . "\n";

# defined functions still override the __call
print $a->getWeight() . "\n";

# trying to get something that doesn't exist returns false
print $a->getNothing();

# this still gets error:
print $a->hotdog();

?>

[#63] DevilDude at darkmaker dot com [2004-09-22 19:57:33]

Php 5 has a simple recursion system that stops you from using overloading within an overloading function, this means you cannot get an overloaded variable within the __get method, or within any functions/methods called by the _get method, you can however call __get manualy within itself to do the same thing.

上一篇: 下一篇: