set_include_path() 有人可以帮我纠正一下吗
我已经浏览过有关此主题的网站,但我仍然感到困惑。我已经解决了我的问题,但我内心深处知道我有点受骗了,这个解决方案可能会导致我进一步出现问题。让我解释一下我在哪里:
这是一个像这样的目录结构文件夹>> [文件夹名称]:
/site root
[ajax-loads]
showorder.php
[bcls] << For bespoke classes
class.order.php
[cls] << For classes
class.main.php
class.db.php
class.sql.php
...
...
dashboard.php
index.php
config.php
...
.. etc etc
所以上面我展示了我有 2 个文件夹,其中包含可供我使用的类,其中大多数是通过 class.main.php 中的 __autoload 调用的。我遇到的问题是当我使用 jQuery 将新内容“load()”到 div 中时;行为发生变化,自动加载的路径是相对于 ajax-load 文件而不是相对于 class.main.php。目前我有以下代码,我觉得我已经作弊了。
//PATHS & AUTOLOAD
set_include_path("./cls");
set_include_path("../cls");
set_include_path("./bcls");
set_include_path("../bcls");
//INVESTIGATE THIS NONESENSE ABOVE!
function __autoload($class_name)
{
require_once 'class' . '.' . $class_name . '.' . 'php';
}
在文件的前面,我的 config.php 文件也遇到了同样的问题,我已经临时修复了它:
set_include_path("./");
set_include_path("../");
现在让我指出,这是我决定摆脱程序编码的第一个大项目,这是我第一次真正的体验自动加载。我真的需要更好地理解这一点,而不是成为一个复制粘贴编码员;我觉得我的理解出现了差距。除了正在更正的代码之外,是否有人能解释一下正确的代码在做什么,因为我已经尝试了一些“解决方案”,但在模仿其他人的示例后我仍然得到奇怪的结果。
爱和亲吻 安迪
I've been through the site regarding this subject but I'm still confused. I have fixed my problem but I know deep down I have kind of cheated and it's a fix that could cause me problems further down the line. Let me explain where I am:
Here is a directory structure folders like this >> [folder name] :
/site root
[ajax-loads]
showorder.php
[bcls] << For bespoke classes
class.order.php
[cls] << For classes
class.main.php
class.db.php
class.sql.php
...
...
dashboard.php
index.php
config.php
...
.. etc etc
So above I'm showing I have 2 folders that contain classes for me to use of which most are called via an __autoload in class.main.php. The problem I am having is when I use jQuery to 'load()' new content in to a div; the behaviour changes and the path to the auto load is relative to the ajax-load file rather than relative to class.main.php. At the moment I have the following code where I feel I have cheated so to speak.
//PATHS & AUTOLOAD
set_include_path("./cls");
set_include_path("../cls");
set_include_path("./bcls");
set_include_path("../bcls");
//INVESTIGATE THIS NONESENSE ABOVE!
function __autoload($class_name)
{
require_once 'class' . '.' . $class_name . '.' . 'php';
}
Earlier in the file I am having the same issue with my config.php file which I have temp fixed with:
set_include_path("./");
set_include_path("../");
Now let me point out this is my first big project having decided to get away from procedural coding and this is my first real experience with autoload. Rather than be a copy paste coder I really need to understand this better; I have hit a gap in my understanding here I feel. As well as the code being corrected, would someone kindly explain what the correct code is doing as I have tried a few 'solutions' but I am still getting odd results after mimicking other people's examples.
Love and kisses
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您确实想修改包含路径,那么您应该像这样使用它:
尽管如此,我还是建议您重命名文件夹。强烈建议不要缩写变量、类、函数、文件夹等。您是否考虑过使用某种 PHP 框架?这样做可能会对您的努力有所帮助,并且您可以避免重新发明轮子。
如果您想要轻量级,请考虑使用 Kohana 或 CodeIgniter。否则我推荐 Zend Framework。
If you really want to modify the include path then you should use it like:
Although, I would recommend that you rename your folders. It is strongly discouraged to abbreviate variables, classes, functions, folders, etc like that. Have you considered using a PHP framework of some sort? Doing so might help you in your endeavor, and you could avoid reinventing the wheel.
If you want light-weight, then consider using Kohana or CodeIgniter. Otherwise I recommend Zend Framework.