07c08-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 39 行

PHP
39
字号
<?php// Define a class that will store values for any property it is given.class Data_Holder {    // The data storage array    private $data = array();    // A method to set any variable passed to this object.      // Store it in the array    public function __set($name, $value) {        $this->data[$name] = $value;    }        // A method to return any values that are set:    public function __get($name) {        if (isset($this->data[$name])) { return $this->data[$name]; }    }    // A method that will allow 'isset' to work on these variables    public function __isset($name) {        return isset($this->data[$name]);    }        // Finally a method to allow 'unset' calls to work on these variables    public function __unset($name) {        unset($this->data[$name]);    }}// Create a data holding object & store some data in it:$data = new Data_Holder();$data->name = 'Francesca';// Now echo the value out to see that the __get method is working:echo "<p>The data value of 'name' is {$data->name}</p>";// Now unset the value, and then use isset to ensure that it happened.unset($data->name);echo '<p>The value is ', isset($data->name) ? '' : 'not ', 'set.</p>';?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?