[英]Can not call global function from php class
I have a php project that uses some functional elements, and some OOP elements, but it seems mixing the two is causing problems. 我有一个PHP项目,它使用了一些功能元素和一些OOP元素,但似乎混合两者导致问题。 Here are the files that are causing the errors: 以下是导致错误的文件:
DB.php db.php中
<?php
function parse_db_entry($from, &$to){
//Function code here
}
?>
User.php user.php的
<?php
require_once 'DB.php';
class User{
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
parse_db_entry($entry, $user);
return $user;
}
}
?>
Everything works as it should, except the call to parse_db_entry
which throws: 一切都按parse_db_entry
工作,除了调用parse_db_entry
抛出:
Fatal error : Call to undefined function parse_db_entry() 致命错误 :调用未定义的函数parse_db_entry()
I am able to access other things in DB.php , for instance if I made a class it there I am able to instantiate it without error, and if I move the function into User.php , it is functional as well. 我能够访问DB.php中的其他内容,例如,如果我在那里创建了一个类,我可以无错误地实例化它,如果我将该函数移动到User.php中 ,它也是有用的。 So what am I doing wrong? 那么我做错了什么? Why can't I call this method? 为什么我不能称这种方法?
I've figured it out! 我已经明白了! Thanks to everyone who had ideas, but it seems the problem was something else. 感谢所有有想法的人,但似乎问题是其他的。
When calling require_once 'DB.php'
, php was actually getting the file: 当调用require_once 'DB.php'
,php实际上是在获取文件:
C:\\xampp\\php\\pear\\DB.php C:\\ XAMPP \\ PHP \\梨\\ db.php中
instead of mine. 而不是我的。
This may be a problem exclusive to XAMPP, but a simple rename of my file to DBUtil.php
fixed everything. 这可能是XAMPP专有的问题,但是我的文件到DBUtil.php
的简单重命名修复了所有问题。
This is a stretch, and I'm totally taking a shot in the dark here, but... 这是一个延伸,我完全在黑暗中拍摄,但......
Are you sure parse_db_entry
is in the global or User's namespace? 您确定parse_db_entry
位于全局或用户名称空间中吗? Note: I added a few lines here and there for testing/debugging. 注意:我在这里和那里添加了几行用于测试/调试。
DB.php: db.php中:
<?php
namespace anotherWorld; // added this ns for illustrative purposes
function parse_db_entry($from, &$to){
echo 'called it';
}
?>
User.php: user.php的:
<?php
namespace helloWorld; // added this ns for illustrative purposes
class User {
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
parse_db_entry($entry, $user);
return $user;
}
}
?>
test.php: test.php的:
<?php
require_once 'DB.php';
require_once 'User.php';
use helloWorld\User;
$a = new User();
$a->load_user('email','pass');
echo 'complete';
?>
Yields Fatal error: Call to undefined function helloWorld\\parse_db_entry() in User.php on line 13
, however when removing the NS declaration in DB.php ( namespace anotherWorld
) thereby putting parse_db_entry
in global NS it runs just fine. 产生Fatal error: Call to undefined function helloWorld\\parse_db_entry() in User.php on line 13
,但是当在DB.php( namespace anotherWorld
)中删除NS声明时,将parse_db_entry
置于全局NS中,它运行得很好。
To verify, use the __NAMESPACE__
constant . 要验证,请使用__NAMESPACE__
常量 。
If namespace is a problem, without compromising DB's namespace, here is an updated User.php: 如果命名空间是一个问题,而不影响DB的命名空间,这里是一个更新的User.php:
<?php
namespace helloWorld;
use anotherWorld; // bring in the other NS
class User {
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
anotherWorld\parse_db_entry($entry, $user); // call the method from that NS
return $user;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.