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

📄 07c07-1.php

📁 介绍PHP5的给类型函数应用
💻 PHP
字号:
<?php// Define an interface that will allow conversion of objects to stringsinterface conversionOptions {    public function asString();    public function asHTML();}// Define a 'cat' class that implements the interfaceclass cat implements conversionOptions {    // Properties of a cat    public $name;    public $color;    // Constructor that requires a name    public function __construct($namesake) {        $this->name = $namesake;    }    // Implement the string function of the interface    public function asString() {        return "Cat - Name: {$this->name}, Color: {$this->color}";    }        // Implement the HTML function of the interface    public function asHTML() {        return "<ul><li>Cat</li><ul><li>Name: {$this->name}</li><li>Color: {$this->color}</li></ul></ul>";    }}// Define class that implements the interface, for anacronym definitionsclass anacronym implements conversionOptions {    // Properties of an anacronym    private $name;    private $definition;    // Constructor that requires the term entry and definition    public function __construct($entry, $def) {        $this->name = $entry;        $this->definition = $def;    }    // Implement the string function of the interface    public function asString() {        return "{$this->name}: {$this->definition}";    }        // Implement the HTML function of the interface    public function asHTML() {        return "<dl><dt>{$this->name}</dt><dd>{$this->definition}</dd></dl>";    }}// Instantiate a cat & give it data$my_cat = new cat('Wiley');$my_cat->color = 'grey tabby';// Now return the cat as a string & echo it:$catstring = $my_cat->asString();echo "<pre>{$catstring}</pre>\n";// And do the same as HTMLecho $my_cat->asHTML();// Now construct an anacronym definition$my_anac = new anacronym('SCA', 'Society for Creative Anacronism');// Covert it into string form:$anac = $my_anac->asString();echo "<pre>{$anac}</pre>\n";// Now output as HTMLecho $my_anac->asHTML();?>

⌨️ 快捷键说明

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