×

面向对象编程 设计模式

PHP面向对象——工厂模式

feihu1996 feihu1996 发表于2018-08-04 17:54:20 浏览253 评论0

抢沙发发表评论

<?php
/*
* @file: example17.php
* @brief: PHP面向对象——工厂模式
* @author: feihu1996.cn
* @date: 下午16:56 18-8-4
* @version: 1.0
*/

class Redis {
    public function get($k){
        echo "get $k";
        echo "\r\n";
    }

    public function set($k, $v){
        echo "set $k $v";
        echo "\r\n";
    }

    public function delete($k){
        echo "delete $k";
        echo "\r\n";
    }
}

class Cache {
    public static function factory() {
        return new Redis();
    }
}

$cache = Cache::factory();
$cache->get("hello");
$cache->set("hello", "world");
$cache->delete("hello");

群贤毕至

访客