⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 07c08-2.php

📁 介绍PHP5的给类型函数应用
💻 PHP
字号:
<?php// Define a class that overloads a variable name by allowing the same//  name to hold a number, and text.class Multi_Data_Holder {    // variables to hold various versions of the same named property    private $number = array();    private $text = array();        // Define __set, which stores data based upon the type of value.    public function __set($name, $value) {        // Check if it is one of the types we are allowing, and store        //  appropriately. If it is an int or float, store it as a number:        if (is_int($value) || is_float($value)) {            $this->number[$name] = $value;        } elseif (is_string($value)) {            $this->text[$name] = $value;        }    }        // Defining isset to understand the 'multi' nature of values here.    public function __isset($name) {        // If it exists in either case, claim it exists.        return isset($this->number[$name]) || isset($this->text[$name]);    }        // Define a method such that unset will unset all values.    public function __unset($name) {        unset($this->number[$name]);        unset($this->text[$name]);            }    // It won't be possible to define a get that knows which one to return,    //  so let's define a function instead that takes a second parameter of    //  either 'number' or 'text' to determine which one to read:    public function value($name, $type) {        // Use the $type as a variable variable to return the right value:        return $this->{$type}[$name];    }}// Create a data holding object$data = new Multi_Data_Holder();// Store a number, and the English representation of the number$data->x = 173;$data->x = 'One Hundred Seventy Three';// Now echo the number and it's text:echo '<p>The number ', $data->value('x', 'number'), ' is written: ',    $data->value('x', 'text'), '</p>';// Now unset the value, and then use isset to ensure that it happened.unset($data->x);echo '<p>The value is ', isset($data->x) ? '' : 'not ', 'set.</p>';?>

⌨️ 快捷键说明

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