News for hackers. Stuff that matters.
Re:New Features in PHP 5.6 by krapp on Friday January 17, 2014 @03:59PM (#7078368)
I agree with you. This is the closest i've been able to get...

   class whatever{
     
     private $Properties=array();

     function __set($key,$val){
        try{
         if(!isset($this->Properties[$key])){
            $this->Properties[$key]=$val;
         }
        }catch(Exception $e){
        //...
        }
      }

      function __get($key){
       try{
        if(isset($this->Properties[$key])){
          return $this->Properties[$key];
        }
       }catch(Exception $e){
       //...
       }
     }
    }
Re:New Features in PHP 5.6 by datphp on Friday January 17, 2014 @08:10PM (#7079569)
Your code doesn't work...

The catch will never be called, and you should never use isset() because it will return false if the value exists but is null.

    whatever->param = null;
    whatever->param = 123;
Both lines will execute here.

    function __set($k, $v){
        if (array_key_exists($k, $this->Properties)){
            throw new ReadOnlyException();
        }else{
            $this->Properties[$k] = $v;
        }
    }
Re:New Features in PHP 5.6 by krapp on Friday January 17, 2014 @08:13PM (#7079580)
Yes, thank you. I noticed the problem after it was too late to edit. Slightly embarrassing.
Re:New Features in PHP 5.6 by krapp on Friday January 17, 2014 @06:07PM (#7079108)
I just noticed some errors in this but the basic gist works... I shouldn't have added the isset checks, those would prevent it from throwing the exceptions -_-