English 中文(简体)
在PHP中建立动态工厂方法(<5.3)
原标题:Create dynamic factory method in PHP (< 5.3)

如何在家用植物卫生体系中创造有活力的工厂方法? 通过动态工厂方法,我指的是工厂方法,根据特定论点的某些方面,可以自动查明哪些物体。 最好不首先在工厂登记。 拥有可能物体的Im OK 位于一个共同地点(目录)。

我想避免你在工厂方法上的典型开关,例如:

public static function factory( $someObject )
{
    $className = get_class( $someObject );
    switch( $className )
    {
        case  Foo :
            return new FooRelatedObject();
            break;
        case  Bar :
            return new BarRelatedObject();
            break;
        // etc...
    }
}

我的具体案例涉及工厂,根据该项目建立一个投票存放处。 这些项目都安装了<代码>可检索接口。 与此类似:

Default_User implements Voteable ...
Default_Comment implements Voteable ...
Default_Event implements Voteable ...

Default_VoteRepositoryFactory
{
    public static function factory( Voteable $item )
    {
        // autodiscover what type of repository this item needs
        // for instance, Default_User needs a Default_VoteRepository_User
        // etc...
        return new Default_VoteRepository_OfSomeType();
    }
}

我希望能够放弃这些物品的新可溶解物品和烟雾储存库,同时不影响工厂的实施。

最佳回答

如果开关声明被删除,你基本上改用命名公约。 采用所穿的那类物品,并用它来瞬息万变。 简单例子如下。

//pseudo code, untested
class Default_VoteRepositoryFactory
{
    public static function factory( Voteable $item, $arg1, $arg2 )
    {
        $parts = explode( _ , get_class($item));
        $type  = array_pop();
        $class =  Default_VoteRepository_  . $type;
        return new $class($arg1, $arg2);
        // autodiscover what type of repository this item needs
        // for instance, Default_User needs a Default_VoteRepository_User
        // etc...
    }
    //can be as complex or as simple as you need
    protected static function getType(stdClass $item)
    {
        $parts = explode( _ , get_class($item));
        return array_pop($parts);       
    }
}
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签