[ create a new paste ] login | about

Link: http://codepad.org/ZOm8cOzd    [ raw code | output | fork ]

PHP, pasted on Feb 17:
<?php

abstract class xGrandparent {
    public function __construct( $params = array() ){
        foreach( $params as $k => $v ){
            if( property_exists($this, $k) ){
                $this->$k = $v;
            } else {
                switch($k){
                    case 'e':
                        throw new Exception('exception e', 111);
                        break;
                    case 'f':
                        throw new Exception('exception f', 222);
                        break;
                }
            }
        }
    }
}

abstract class xParent extends xGrandparent {
    protected
        $c,
        $d
    ;

    public function __construct( $params = array() ){
        try {
            parent::__construct( $params );
        } catch ( Exception $e ){
            if( $e->getCode() == 222 ){
               echo "*** parent caught exception \n";
            } else {
               throw $e;
            }
        }
    }
}

class xChild extends xParent {
    protected 
        $a,
        $b
    ;

    public function __construct( $params = array() ){
        try {
            parent::__construct( $params );
        } catch ( Exception $e ){
            echo "*** child caught exception \n";
        }
    }

}


$test = new xChild(array(
    'a' => 'a val',
    'c' => 'c val',
    'e' => 'e val',
    'f' => 'f val',
    'b' => 'b val',
    'd' => 'd val',

));

var_dump( $test );


Output:
1
2
3
4
5
6
7
8
9
10
11
*** child caught exception 
object(xChild)#1 (4) {
  ["a:protected"]=>
  string(5) "a val"
  ["b:protected"]=>
  NULL
  ["c:protected"]=>
  string(5) "c val"
  ["d:protected"]=>
  NULL
}


Create a new paste based on this one


Comments: