建立一个“工厂"在PHP中 [英] Building a "factory" in PHP

查看:91
本文介绍了建立一个“工厂"在PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个File类,该类负责对文件,I/O的所有操作,并且根据文件的性质而采取不同的操作.我对它的实际结构不满意,它看起来像这样:

I have created a File class, which takes care of all operations on files, I/O, and which acts differently depending on the nature of the files. I'm not happy with its actual structure, which looks like this:

    class File
    {
        function __construct($id)
        {
            $bbnq = sprintf("
                SELECT *
                FROM documents
                WHERE id = %u",
                $id);
            $req = bbnf_query($bbnq);
            $bbn = $req->fetch();
            $this->file_type = $bbn['file_type'];
            $this->file_name = $bbn['file_name'];
            $this->title = $bbn['title'];
        }
        function display()
        {
            return '<a href="'.$this->file_name.'">'.$this->title.'</a>';
        }
    }

    class Image extends File
    {
        function __construct($id)
        {
            global $bbng_imagick;
            if ( $bbng_imagick )
                $this->imagick = true;
            parent::__construct($id);
        }
        function display()
        {
            return '<img src="'.$this->file_name.'" alt="'.$this->title.'" />';
        }
    }

在这里,我需要首先了解文件类型,以确定要使用的类/子类.
我想实现相反的效果,即向我的班级发送一个ID,该ID返回与文件类型相对应的对象.
我最近更新到PHP 5.3,并且我知道有一些新功能可用于创建工厂"(静态绑定吗?).我对OOP的知识很浅,所以我想知道是否有人对结构提出建议,以便制作一个可以调用正确构造函数的唯一类.

Here I need first to know the file type in order to determine which class/subclass to use.
And I'd like to achieve the opposite, i.e. send an ID to my class, which returns an object corresponding to the file type.
I have recently updated to PHP 5.3, and I know there are some new features which could be of use for creating a "factory" (late static bindings?). My OOP knowledge is pretty light, so I wonder if some have structural suggestions in order to make a unique class which will call the right constructor.

谢谢!

推荐答案

我不认为后期静态绑定与此处相关-工厂模式不需要它们.试试这个:

I don't think late static bindings is relevant here - a factory pattern doesn't require them. Try this:

class FileFactory
{
    protected static function determineFileType($id) 
    {
        // Replace these with your real file logic
        $isImage = ($id>0 && $id%2);
        $isFile = ($id>0 && !($id%2));

        if ($isImage) return "Image";
        elseif ($isFile) return "File";
        throw new Exception("Unknown file type for #$id");
    }

    public static function getFile($id) {
        $class = self::determineFileType($id);
        return new $class($id);
    }
}

// Examples usage(s)
for ($i=3; $i>=0; $i--) {
    print_r(FileFactory::getFile($i));
}

顺便说一句,无论您认为它有多安全,您都绝对应该从DB逃脱输出.例如,在标题中用双引号进行测试(更不用说恶意输入了).

As an aside, you should definitely escape your output from the DB, no matter how safe you think it is. Test with double quotes in a title, for example (let alone more malicious input).

如果它是项目的一部分,那么您可能希望将View层(您的HTML输出)与此Model层分开,即实现

Also if it's part of a project, you might want to separate the View layer (your HTML output) from this Model layer, ie implement MVC...

这篇关于建立一个“工厂"在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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