简体   繁体   English

使用静态工厂模式时,包含PHP库的最佳方法是什么?

[英]What is the best way to include PHP libraries when using static factory pattern?

I have several static factory patterns in my PHP library. 我的PHP库中有几个静态工厂模式。 However, memory footprint is getting out of hand and we want to reduce the number of files required during execution time. 但是,内存占用空间不大,我们希望减少执行期间所需的文件数量。 Here is an example of where we are today: 以下是我们今天的例子:

require_once('Car.php');
require_once('Truck.php');

abstract class Auto
{
    // ... some stuff ...

    public static function Create($type)
    {
        switch ($type) {
            case 'Truck':
                return new Truck();
                break;
            case 'Car':
            default:
                return new Car();
                break;
        }
    }
}

This is undesirable because Car.php AND Truck.php will need to be included even though only one or the other may be needed. 这是不可取的,因为即使只需要一个或另一个,也需要包含Car.php和Truck.php。 As far as I know, require/include and their ..._once variation include libraries at the same scope as it's call. 据我所知,require / include和他们的......一旦变化包括与调用相同范围的库。 Is this true? 这是真的?

If so, I believe this would lead to a memory leak: 如果是这样,我相信这会导致内存泄漏:

    abstract class Auto
    {
        // ... some stuff ...

        public static function Create($type)
        {
            switch ($type) {
                case 'Truck':
                    require_once('Truck.php');
                    return new Truck();
                    break;
                case 'Car':
                default:
                    require_once('Car.php');
                    return new Car();
                    break;
            }
        }
    }

It looks to me that in the 2nd example, multiple calls to Create() would lead to multiple requires because of the scope of the call even though the require_once flavor is used. 在我看来,在第二个例子中,即使使用了require_once风格,多次调用Create()也会因为调用范围而导致多个需求。

Is this true? 这是真的? What is the best way to include libraries dynamically in php in an example such as these? 在这样的示例中,在php中动态包含库的最佳方法是什么?

Thanks! 谢谢!

The Autoload facility is often seen as Evil, but it works at these task quite nicely. Autoload设施通常被视为邪恶,但它很适合这些任务。

If you can get a good filesystem <-> classname mapping so you can, when given a class to provide, find it, then it will save you overhead and only load classes when needed. 如果你可以得到一个好的文件系统< - > classname映射,那么你可以在给定一个提供的类时找到它,那么它将节省你的开销,只在需要时加载类。

It works for static classes too, so once the static class is in scope, it doesn't need to even call the "is file included yet" test of require once, because the Class is already in the symbol table. 它也适用于静态类,所以一旦静态类在范围内,它甚至不需要调用require的“is yet included yet”测试,因为Class已经在符号表中。

Then you can just create 然后你就可以创建了

require("autoloader.php"); 
$x = new Car();  
$x = new Bike(); 

and it will just bring them in when needed. 并且它会在需要时将它们带入。

See Php.net/__autoload for more details. 有关详细信息,请参阅Php.net/__autoload

I would recommend using the autoloader . 我建议使用自动加载器

That is, don't use require_once() to require either subclass, but allows the autoloader to call a function which can load the referenced class when you call new Truck() or new Car() . 也就是说,不要使用require_once()来要求任何子类,但允许自动加载器调用一个函数,该函数可以在调用new Truck()new Car()时加载引用的类。

As for the memory leak question, no, require_once is not scoped by the code block. 至于内存泄漏问题,不, require_once不是代码块的范围。

看一下__autoload()函数

The point of the require_once function is that no matter the scope, you won't include a file twice and redefine a class causing a PHP error. require_once函数的要点是,无论作用域如何,您都不会包含两次文件并重新定义导致PHP错误的类。 So no worries about memory leaks, if the require_once is hit, the class def goes in the global symbol table only one time. 所以不用担心内存泄漏,如果命中require_once,类def只会在全局符号表中出现一次。

But aside from that, yeah use autoloader. 但除此之外,是的,使用自动加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM