类声明的范围

发布于 2024-09-12 06:27:43 字数 887 浏览 8 评论 0原文

我有一个文件 classes.php,其中声明了一组要在一系列页面中使用的类。 然后,在一个页面中,我需要使用另一个类,该类在单独的文件中声明。现在,这个单独的类使用位于 classes.php 文件中的一些类。

所以,在大多数页面中,我都有:

[1]
<?php
require('classes.php');
[page code]
?>

效果很好。问题出在该特定页面上,该页面具有:

[2]
<?php
require('classes.php');
require('separate_class.php');
[page code]
?>

并且这不起作用,因为 separate_class.php 还需要 classes.php

如果我尝试这样做:

[3]
<?php
require('separate_class.php');
[page code]
?>

那么它不起作用,因为现在这些类不可用于此页面,即使它们是在 separate_class.php 文件中声明的。

明显的解决方案似乎是在 classes.php 文件中包含单独的类,这是笨拙的,因为这个单独的类仅在这一系列页面中有意义,而文件 classes.php 中的类仅在这一系列页面中有意义。 php 包含通用类并在各种应用程序之间共享。

我怎样才能以一种允许我保持如[2]所示的结构的方式来做到这一点,即将通用类与特定类分开,而特定类又需要这些通用类,同时仍然能够在页面中使用这些通用类?

I have a file classes.php in which I declare a set of classes to be used in a series of pages.
Then, in one page, I need to use another class, that is declared in a separate file. Now, this separate class uses some classes located in the classes.php file.

So, in most pages, I have:

[1]
<?php
require('classes.php');
[page code]
?>

which works fine. The problem comes with that specific page, which has:

[2]
<?php
require('classes.php');
require('separate_class.php');
[page code]
?>

and this does not work because separate_class.php also requires classes.php.

If I try this:

[3]
<?php
require('separate_class.php');
[page code]
?>

then it does not work because now the classes are not available for this page, even if they are declared inside the separate_class.php file.

The obvious solution seems to be the inclusion of the separate class in the classes.php file, which is clumsy because this separate class makes sense only in this series of pages, while the file classes.php contains general purpose classes and is shared among various applications.

How can I do it in a way that allows me to keep a structure like shown in [2], that is, separating general-purpose classes from a specific class which, in turn, requires those general-purpose classes, while still being able to use these general-purpose classes in the page?

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

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

发布评论

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

评论(2

靖瑶 2024-09-19 06:27:43

更合理的解决方案可能是将每个类分成它自己的文件并使用自动加载器...

http://php.net/manual/en/function.spl-autoload.php

A more sound solution may be to seperate each class into it's own file and use an autoloader...

http://php.net/manual/en/function.spl-autoload.php

岁月流歌 2024-09-19 06:27:43

您想使用 require_once()

编辑: 如果您想要这样做,则无法重新声明类。我不知道你的代码是什么样的,但也许继承就是你的正在寻找?

编辑2:当我在separate_class.php中有一个require'classes.php'并且在您实际打开的脚本。因此,您有 2 个选择:

  • 在 requireseparate_class.php 之前,不要在脚本中 require(或 require_once)classes.php
  • 在任何地方使用 require_once(检查separate_class.php)并且不必担心是否需要文件。

You want to use require_once()

EDIT: You can't redeclare a class if that's what you are trying to do. I don't know what your code looks like, but maybe inheritance is what you are looking for??

EDIT 2: I have only been able to reproduce your error when I had a require 'classes.php' in separate_class.php AND a require 'classes.php' in the script you are actually opening. So you have 2 options:

  • Don't require (or require_once) classes.php in your script before you have required separate_class.php
  • Use require_once everywhere (check separate_class.php) and don't worry about whether or not a file has been required.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文