Return to Snippet

Revision: 22101
at January 1, 2010 13:59 by mpcircuitry


Updated Code
class AbstractModel {
        private $data = array();
       
      protected function __set($name, $value) {
        $this->data[$name] = $value;
      }
       
      protected function __get($name) {
        if (array_key_exists($name, $this->data)) {
           return $this->data[$name];
        }
        return null;
      }
       
      protected function __isset($name) {
        return isset($this->data[$name]);
      }
       
      protected function __unset($name) {
        unset($this->data[$name]);
       }
    
    public function printOut()
    {
        echo "<pre>";
        print_r($this);
        echo "</pre>";
    }
}
class User extends AbstractModel {

}

// how to use

$person = new User();

$person->name = 'John';
$person->age = 26;
$person->address = "217 Ziller's road brooklyn NY 11553";
$person->printOut();

$car = new User();
$car->make = 'Honda';
$car->model = 'Civic';
$car->cylinder = 6;
$car->transmission = 'Automatic';
$car->year = 2006;

$truck = new User();
$truck->make = 'Ford';
$truck->model = 'F-150';
$truck->cylinder = 12;
$truck->transmission = 'Manual';
$truck->year = 2006;


$person->car = array('honda' =>$car, 'Ford' => $truck);

$person->printOut();


//output
User Object
(
    [data:private] => Array
        (
            [name] => John
            [age] => 26
            [address] => 217 Ziller's road brooklyn NY 11553
        )

)

User Object
(
    [data:private] => Array
        (
            [name] => John
            [age] => 26
            [address] => 217 Ziller's road brooklyn NY 11553
            [car] => Array
                (
                    [honda] => User Object
                        (
                            [data:private] => Array
                                (
                                    [make] => Honda
                                    [model] => Civic
                                    [cylinder] => 6
                                    [transmission] => Automatic
                                    [year] => 2006
                                )

                        )

                    [Ford] => User Object
                        (
                            [data:private] => Array
                                (
                                    [make] => Ford
                                    [model] => F-150
                                    [cylinder] => 12
                                    [transmission] => Manual
                                    [year] => 2006
                                )

                        )

                )

        )

)

Revision: 22100
at January 1, 2010 13:58 by mpcircuitry


Initial Code
class AbstractModel {
        private $data = array();
       
      protected function __set($name, $value) {
        $this->data[$name] = $value;
      }
       
      protected function __get($name) {
        if (array_key_exists($name, $this->data)) {
           return $this->data[$name];
        }
        return null;
      }
       
      protected function __isset($name) {
        return isset($this->data[$name]);
      }
       
      protected function __unset($name) {
        unset($this->data[$name]);
       }
    
    public function printOut()
    {
        echo "<pre>";
        print_r($this);
        echo "</pre>";
    }
}

//output
User Object
(
    [data:private] => Array
        (
            [name] => John
            [age] => 26
            [address] => 217 Ziller's road brooklyn NY 11553
        )

)

User Object
(
    [data:private] => Array
        (
            [name] => John
            [age] => 26
            [address] => 217 Ziller's road brooklyn NY 11553
            [car] => Array
                (
                    [honda] => User Object
                        (
                            [data:private] => Array
                                (
                                    [make] => Honda
                                    [model] => Civic
                                    [cylinder] => 6
                                    [transmission] => Automatic
                                    [year] => 2006
                                )

                        )

                    [Ford] => User Object
                        (
                            [data:private] => Array
                                (
                                    [make] => Ford
                                    [model] => F-150
                                    [cylinder] => 12
                                    [transmission] => Manual
                                    [year] => 2006
                                )

                        )

                )

        )

)


class User extends AbstractModel {

}

// how to use

$person = new User();

$person->name = 'John';
$person->age = 26;
$person->address = "217 Ziller's road brooklyn NY 11553";
$person->printOut();

$car = new User();
$car->make = 'Honda';
$car->model = 'Civic';
$car->cylinder = 6;
$car->transmission = 'Automatic';
$car->year = 2006;

$truck = new User();
$truck->make = 'Ford';
$truck->model = 'F-150';
$truck->cylinder = 12;
$truck->transmission = 'Manual';
$truck->year = 2006;


$person->car = array('honda' =>$car, 'Ford' => $truck);

$person->printOut();

Initial URL


Initial Description
This is a class/demonstration for creating dynamic properties and storing them in an array

Initial Title
Dynamic PHP Properties

Initial Tags
php, array

Initial Language
PHP