duanhao5038
2016-10-16 21:47
浏览 11
已采纳

包含实例和文件的最佳方法是什么?

I am working on a php application. I have many classes and files divided into different directories. I have a config file that holds the database connection settings and files with require. For example:require "core/application.php" and I do this for all my classes and files. config.php also creates my objects. I tried to require config.php in all my files so that my classes would have access to the objects in config.php but it tries to create another object and I get and error saying that the object already exists. I also tried to create an init() function and call it in my index.php but then I get an error saying that init() already exists. I was trying to auto load my classes but I don't think that is going to work. I have been trying different things for the past few days but I cannot figure anything out. I also don't want to pass my classes the objects. Thank you for the help.

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个php应用程序。 我有很多类和文件分为不同的目录。 我有一个配置文件,其中包含 require 的数据库连接设置和文件。 例如:需要“core / application.php”,我为所有类和文件执行此操作。 config.php 也会创建我的对象。 我试图在我的所有文件中都要求 config.php ,以便我的类可以访问 config.php 中的对象,但它尝试创建另一个对象,我得到了 错误说该对象已存在。 我还试图创建一个 init()函数并在我的 index.php 中调用它,但后来我得到一个错误,说 init() 已经存在。 我试图自动加载我的课程,但我认为这不会起作用。 过去几天我一直在尝试不同的事情,但我无法弄明白。 我也不想把我的类传给对象。 感谢您的帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxun4110 2016-10-16 22:15
    已采纳

    instead of require use require_once

    点赞 打赏 评论

相关推荐 更多相似问题