PHP设计模式 [英] PHP Design Pattern

查看:119
本文介绍了PHP设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类执行数据库操作并返回结果(数组,真,假)。而且我有一个类可以在其构造函数中使用这个类创建JSON字符串。
我们可以说这个类是适配器吗?或简单的包装或...

 类Db 
{
public function getRows($ params)
{
// ...
}
}


类DbAdapter
{
private $ _dbh;
public function __construct($ dbh)
{
$ this-> _dbh = $ dbh;
}

public function getJson()
{
return'{key:foo,key2:'。 json_encode($ this-> _dbh-> getRows($ params))。 };
}
}

谢谢

解决方案

Id说它更多的是Decorator ... http://en.wikipedia.org/wiki/Decorator_pattern


I have a class that performs database operations and returns results (array, true, false). And I have an another class that creates JSON string by using this class in its constructor. Can we say this class is an Adapter? Or simply wrapper or ...

Class Db
{
    public function getRows($params)
    {
        //...
    }
}


Class DbAdapter
{
    private $_dbh;
    public function __construct($dbh)
    {
        $this->_dbh = $dbh;
    }

    public function getJson()
    {
        return '{"key": "foo", "key2": ' . json_encode($this->_dbh->getRows($params)) . '}';
    }
}

Thanks

解决方案

Id say its more of Decorator... http://en.wikipedia.org/wiki/Decorator_pattern

这篇关于PHP设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆