将类加载到函数中?
我目前正在编写一个脚本,我遇到了以下情况。
function somnicefunction()
{
require 'someexternalclass.php';
$somevar = new SomeExternalClass();
}
由于某种原因,上述内容破坏了该功能。我不知道为什么,我在 php.net 中没有看到太多关于此的文档,而且 google 没有返回任何实际结果。 有人知道吗?
I`m currently working on a script, and I have the following situation.
function somnicefunction()
{
require 'someexternalclass.php';
$somevar = new SomeExternalClass();
}
For some reason, the above breaks the function. I'm not sure why, I haven't seen much documentation in php.net regarding this, plus google returned no real results.
Does anyone have any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果多次调用该函数,则尝试包含同一文件可能会遇到错误。
尝试使用
require_once()
代替。除此之外,您的示例本身并没有什么“非法”之处。If you call the function more than once, you may encounter an error by trying to include the same file.
Try using
require_once()
instead. Other than that, there is nothing inherently 'illegal' about your example.您的代码绝对有效。我在本地主机上测试了它,它工作得很好。我使用了以下代码:
function.php
include.php
您确定那里没有任何拼写错误吗?如果您没有收到任何错误,则可能表明您尚未在任何地方使用该函数。
Your code is absolutely valid. I tested it on localhost and it works just fine. I used following piece of code:
function.php
include.php
Are you sure you don't have any typo there? If you aren't getting any error, it might indicate that you haven't used the function anywhere.
尝试先声明对象,然后将其作为参数传递给函数:
Try declaring the object first and then pass it as a parameter to the function: