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

发布于 2024-07-08 14:30:59 字数 1282 浏览 8 评论 0原文

我的 PHP 库中有几个静态工厂模式。 然而,内存占用正在失控,我们希望减少执行期间所需的文件数量。 这是我们今天所处位置的一个示例:

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;
        }
    }
}

这是不可取的,因为即使只需要其中之一,也需要包含 Car.php 和 Truck.php。 据我所知, require/include 及其 ..._once 变体包含与其调用范围相同的库。 这是真的?

如果是这样,我相信这会导致内存泄漏:

    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;
            }
        }
    }

在我看来,在第二个示例中,即使使用了 require_once 风格,由于调用的范围,对 Create() 的多次调用也会导致多次 require 。

这是真的? 在诸如此类的示例中,在 php 中动态包含库的最佳方法是什么?

谢谢!

I have several static factory patterns in my PHP library. 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. As far as I know, require/include and their ..._once variation include libraries at the same scope as it's call. 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.

Is this true? What is the best way to include libraries dynamically in php in an example such as these?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寻梦旅人 2024-07-15 14:30:59

自动加载功能通常被认为是邪恶的,但它在这些任务上工作得相当好。

如果你能得到一个好的文件系统<-> 类名映射,这样当给定一个要提供的类时,您就可以找到它,这样它将节省您的开销,并且仅在需要时加载类。

它也适用于静态类,因此一旦静态类在范围内,它甚至不需要调用一次 require 的“是否包含文件”测试,因为该类已经在符号表中。

然后你就可以创建

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

,它会在需要时将它们引入。

有关更多详细信息,请参阅 Php.net/__autoload

The Autoload facility is often seen as Evil, but it works at these task quite nicely.

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.

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.

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.

浪菊怪哟 2024-07-15 14:30:59

我建议使用自动加载器

也就是说,不要使用 require_once() 来要求任一子类,而是允许自动加载器调用一个函数,该函数可以在您调用 new Truck() 时加载引用的类或new Car()

至于内存泄漏问题,不,require_once 不在代码块的范围内。

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().

As for the memory leak question, no, require_once is not scoped by the code block.

梦萦几度 2024-07-15 14:30:59

require_once 函数的要点是,无论范围如何,您都不会两次包含一个文件并重新定义一个类,从而导致 PHP 错误。 所以不用担心内存泄漏,如果 require_once 被命中,类 def 只会进入全局符号表一次。

但除此之外,可以使用自动加载器。

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. So no worries about memory leaks, if the require_once is hit, the class def goes in the global symbol table only one time.

But aside from that, yeah use autoloader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文