set_include_path() 有人可以帮我纠正一下吗

发布于 2024-12-22 00:32:54 字数 1191 浏览 0 评论 0原文

我已经浏览过有关此主题的网站,但我仍然感到困惑。我已经解决了我的问题,但我内心深处知道我有点受骗了,这个解决方案可能会导致我进一步出现问题。让我解释一下我在哪里:

这是一个像这样的目录结构文件夹>> [文件夹名称]:

/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 技术交流群。

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

发布评论

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

评论(1

守不住的情 2024-12-29 00:32:54

如果您确实想修改包含路径,那么您应该像这样使用它:

define('BASE_PATH', $_SERVER['DOCUMENT_ROOT']);

// ...

set_include_path(
    BASE_PATH . '/bcls' .
    PATH_SEPARATOR . BASE_PATH . '/cls' .
    PATH_SEPARATOR . get_include_path() .
    PATH_SEPARATOR . '.'
);

尽管如此,我还是建议您重命名文件夹。强烈建议不要缩写变量、类、函数、文件夹等。您是否考虑过使用某种 PHP 框架?这样做可能会对您的努力有所帮助,并且您可以避免重新发明轮子。

如果您想要轻量级,请考虑使用 Kohana 或 CodeIgniter。否则我推荐 Zend Framework。

If you really want to modify the include path then you should use it like:

define('BASE_PATH', $_SERVER['DOCUMENT_ROOT']);

// ...

set_include_path(
    BASE_PATH . '/bcls' .
    PATH_SEPARATOR . BASE_PATH . '/cls' .
    PATH_SEPARATOR . get_include_path() .
    PATH_SEPARATOR . '.'
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文