Page 4 of 4

Re: Suggestions for PHP 6

Posted: Tue Mar 10, 2009 10:08 pm
by Chris Corbyn
crazycoders wrote:
Chris Corbyn wrote:Anonymous objects...
snipped out anonymous methods code
Unless wrong, this is part of php6 along with lambda expressions no?
Nope, just anonymous functions in PHP6. No anonymous object support.
crazycoders wrote:
Chris Corbyn wrote:Accessor synthesizers...
snipped out Accessor synthesizers code
Accessor synthesizers are simply get/set methods that are public but exist or not. If you wish to protect a variable from being armed in an object simply create a public function getvariable(){ return $this->privatevariable; } and don't create a setter.
I add this to my base object class that i always use:

Code: Select all

 
class system_object {   
 
    //Constructor of the class
    public function __construct(){}
    
    //Magic property handlers
    public function __get($name){
        if(method_exists($this, 'get'.$name)){ return call_user_func(array($this, 'get'.$name)); }
        else{ throw new Exception('Property '.$name.' not found in '.$this.' ('.get_class($this).') or property is writeonly.'); }
    }
    public function __set($name, $value){
        if(method_exists($this, 'set'.$name)){ call_user_func(array($this, 'set'.$name), $value); }
        else{ throw new Exception('Property '.$name.' not found in '.$this.' ('.get_class($this).') or property is readonly.'); }
    }
 
}
 
The problem with __get() and __set() is that they try do bundle everything into one "god" method. Other languages seem to find much more elegant solutions for this. Yes, the functionality exists, but it's a little rough IMHO.