类声明的范围
我有一个文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更合理的解决方案可能是将每个类分成它自己的文件并使用自动加载器...
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
您想使用 require_once()
编辑: 如果您想要这样做,则无法重新声明类。我不知道你的代码是什么样的,但也许继承就是你的正在寻找?
编辑2:当我在separate_class.php中有一个
require'classes.php'
并且在您实际打开的脚本。因此,您有 2 个选择:require
(或require_once
)classes.phpYou 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:require
(orrequire_once
) classes.php in your script before you have required separate_class.php