gpt4 book ai didi

php oop - 每个类都有不同的文件?导入包? ETC

转载 作者:行者123 更新时间:2023-12-03 01:02:59 27 4
gpt4 key购买 nike

我习惯于使用诸如C#/Java/Python之类的语言,其中每个类都有自己的文件,并且对于class 要查看其他类,您需要导入包含这些类的包。这在 php 中是如何工作的?该文档向您展示了如何创建类,但我不明白它们如何在 php 上下文中组合在一起。我知道 include 语句,它基本上只是将文件粘在一起。

最佳答案

您可以使用__autoload

function __autoload($class_name) {
include 'classes/'.$class_name . '.php';
}

因此,将每个类都放在类文件夹中自己的文件中。当您想使用该类时,它将包含它。更多信息:http://php.net/manual/en/language.oop5.autoload.php

更新:当我回答这个问题时,它是完全有效的。现在它仍然有效,但请记住 PHP.net 从那时起是这样说的:

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

关于php oop - 每个类都有不同的文件?导入包? ETC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4758024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com