问题标签 [spl-autoload-register]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1823 浏览

php - spl_autoload 未加载(区分大小写?)

我正在使用 ZF2 作为库。我有一个非常标准的实现,spl_autoload_register如下所示:

在哪里:

在我的 Mac 上,一切正常。在我的 CentOS 服务器上,不。我唯一一次看到spl_autoload_register不喜欢这样的问题是当我尝试使用 CamelCasing 等时。显然,情况并非如此。我正在尝试加载:

现在,我的目录结构如下所示:

你明白了。

那么,为什么这不起作用?

编辑:请告诉它不是在寻找 config.php 而不是 Config.php ...

0 投票
1 回答
10459 浏览

php - spl_autoload_register not working

I have created 5 folders containing 5 classes (Ad_Class, Blocked_Class, Friend_Class, Image_Class, Profile_Class) in the main directory. I also created the respective classes within the mentioned folders with the exact name as the folders. i.e. if folder name is Ad_Class then the class within the folder is also the same as the folder name as in "class Ad_Class.

In the index.php file I wrote the following code:

After executing the above code I get the below warnings:

Why is this happening? Can anyone explain?

Update: I found the following code to work. If you have any suggestions please feel free to share!

It produces the following: Advertising Blocked Friend Image

which is nothing more than displaying the class name of the instantiated object to see if the code worked correclty in loading classes.

0 投票
1 回答
929 浏览

php - 使用 spl_autoload_register 处理单个文件中的多个类

使用spl_autoload_register,可以自动加载类。让我们举一个使用两个文件的简单示例:

索引.php

厨房/烤箱.php

运行$ php index.php给出以下输出:

一切都很好,我唯一关心的是命名法。我*必须*用该文件中任何类的相同名称提名我的文件。有什么方法可以让我使用更通用的名称吗?我希望我可以使用“kitchen/objects.php”而不是“kitchen/oven.php”。

谢谢。

0 投票
3 回答
523 浏览

php - PHP PSR-0 Static Class

I'm trying to rewrite an OO PHP site (that loosely follows an MVC structure) so it uses namespaces - and want to follow PSR-0.

In the current site I have a class (called APP) which is full of static methods that I call all over the place to handle things such as getting config data eg; APP::get_config('key').

Obviously with namespacing, I would need to call \TheNameSpace\App::get_config('key'). I use this class frequently, so want to avoid having to prefix the namespace every time I use it. I do call methods in it from within other classes, which would usually be under a sub-namespace - so changing the namespace at the top of the file won't really work.

So, I guess my question is, what is the easiest way to have a 'global' class with methods that I can call anywhere without having to prefix with the namespace each time?

0 投票
1 回答
499 浏览

php - 尝试自动加载时如何绕过 ini_set disabled

我正在编写一个使用 Raven 的 WordPress 插件,它以下列方式使用自动加载:

但是,该插件的某些用户已被其主机提供商禁用了 ini_set,并且在激活插件时会引发错误:

警告:出于安全原因,ini_set() 已在...中禁用

在这种情况下,有没有办法使用 ini_set ?

我想我真正的问题是使用 spl_autoload_register 时是否需要 ini_set?

0 投票
1 回答
2053 浏览

php - 了解自动加载和 spl_autoload_register

我有一个小的框架,在我的脚本中我使用自动加载(我现在才知道我需要开始在我的脚本中摆脱这个功能)

我现在正在尝试使用 Twilio API,在他们的代码中,他们使用 spl_autoload_register 函数。当我对我的一个类进行新调用时,在脚本的其余部分中调用 twilio 代码的逻辑块发生故障后

我的文件结构是这样的

正在崩溃的脚本看起来像这样

我现在需要在 Classes 文件夹中做什么才能使所有类都正常工作?

0 投票
1 回答
958 浏览

php - spl_autoload_register、SessionHandlerInterface 和 PHPUnit 的问题

我有一个非常简单的类的小单元测试。这是我要测试的课程:

这是 SessionHandler 类:

这是简单的单元测试:

我有以下 phpunit.xml 文件:

...以及以下 bootstrap.php 文件:

目录结构是这样的:

这就是我尝试从命令行运行这个小单元测试的方式(以及我得到的结果):

我认为这将是注册的自动加载功能的问题,所以我决定跳过类自动加载,以防它是 SessionHandlerInterface 只是为了看看会发生什么:

这就是结果:

如果我直接从命令行调用 SessionHandler.php,它不会返回错误:

我做错了什么或我错过了什么?

这只发生在SessionHandlerInterface上。例如,我尝试只运行testGetInstance,使Auth类实现ArrayAccess,它根本没有抱怨。如果我让Auth类实现SessionHandlerInterface它会立即抱怨。

我正在运行 PHP 5.4.13 和 PHP Unit 3.7.19

0 投票
3 回答
1279 浏览

php - PHP - 将 spl_autoload_register 与子文件夹一起使用

所以我使用 spl_autoload_register 来加载我的类。但是我的课程有这个结构:

所以通常当我包括他们时,我是这样做的:

现在使用 spl_autoload_register 我将如何处理子文件夹?我没有能力升级到 php 5.3 来使用命名空间,我需要做这样的事情吗?

然后我用 spl_autoload_register 注册的函数将 _ 炸开,第一部分作为类文件夹,第二部分作为类方法。但是我必须将类名更改为 modules_Module 对吗?如果是这样可以吗?或者有没有更好的方法来做到这一点?

0 投票
2 回答
841 浏览

php - 为什么 php spl_autoload_register 和 ajax 不能一起工作?

这是我的 index.php 文件:

在我的 main.js 文件中,我执行 AJAX 调用:

在 UserController.php 文件中,我有:

当我将我的“加载器”函数放在 UserController 文件中时,它工作正常并且类 BaseController 已加载,但是当该函数位于我的 index.php 文件中并且我进行 AJAX 调用时,我得到:

致命错误:第 13 行的 D:\xampp\htdocs\heliox\controllers\UserController.php 中找不到类“BaseController”。

0 投票
1 回答
690 浏览

php - PHP spl_autoload_register 继承

我有一个名为house.php 的页面和house.class.php 中的一个类。当我在 house.php 中创建该类的实例时,它可以工作。现在我想制作一个chair.php 文件和一个在house.class.php 中扩展House 的类椅子。我应该把那个课放在哪里?spl_autoload_register 找不到我的类,因为它的名称与文件不同。如何改进功能 AutoloadClass ?我应该创建一个chair.class.php 并使用house.class.php 的要求吗?

谢谢