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

📄 07c09-2.php

📁 介绍PHP5的给类型函数应用
💻 PHP
字号:
<?php// Define a class that will overload a method definitionclass Money {    // Define an addition function that handles many different variations:    public function __call($name, $args) {        // If the function name was 'format' then:        if (($name == 'format') && (count($args) == 1)) {            // If the first argument is a string:            if (is_string($args[0])) {                // Call the string version of the formatter:                return $this->_format_string($args[0]);            } elseif (is_numeric($args[0])) {                // If numeric, call the number version                return $this->_format_number($args[0]);            } elseif (is_array($args[0])) {                // Use the array version now.                return $this->_format_array($args[0]);            }        }                // In any other case, we didn't have a proper method call, exit        trigger_error('Invalid Method Access', E_USER_ERROR);    }    // Define the private/internal function for formatting a number as money    private function _format_number($number) {        // First round it to 2 decimal places:        $number = round($number, 2);                // Now output it, with formatting:        return '$' . number_format($number, 2);    }        // Define the private/internal function for formatting a string as money    private function _format_string($str) {        // Clean up the string by removing all chars that are not digits:        $str = preg_replace('/[^0-9.]/', '', $str);                // Ensure that only one decimal separator exists:        $str = preg_replace('/\.([^.]*)(?=\.)/', '$1', $str);        // Now that it is clean, call format number on it:        return $this->_format_number($str);    }    // Define the private/internal function for formatting an array as money    private function _format_array($arr) {        // Loop through the entire array        $new = array();        foreach ($arr as $a) {            // Depending on whether this is a string, call the appropiate             // function to format it, and save it in a new array:            if (is_string($a)) {                $new[] = $this->_format_string($a);            } else {                $new[] = $this->_format_number($a);            }        }                // Now return the new array back:        return $new;    }    }// Create a new object$my_money = new Money();// Examine the output of calling this method in multiple ways:// As a number: $1,234.57echo '<p>Accessed as number: ', $my_money->format(1234.567), '</p>';// As a string with some garbage in it: $7,561.20echo '<p>Accessed as array: ', $my_money->format('big7,56.1.2'), '</p>';// And as an array of these mixed values://   Array ( [0] => $2.22 [1] => $2,345.64 [2] => $42.00 )echo '<p>Accessed as individual numbers: ';print_r($my_money->format(array(2.217, '2-345.64', 42)));echo '</p>';?>

⌨️ 快捷键说明

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