简体   繁体   中英

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. Here are the files that are causing the errors:

DB.php

<?php

function parse_db_entry($from, &$to){
    //Function code here
}

?>

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:

Fatal error : Call to undefined function 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. 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:

C:\\xampp\\php\\pear\\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.

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? Note: I added a few lines here and there for testing/debugging.

DB.php:

<?php

namespace anotherWorld; // added this ns for illustrative purposes

function parse_db_entry($from, &$to){
    echo 'called it';
}

?>

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:

<?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.

To verify, use the __NAMESPACE__ constant .


If namespace is a problem, without compromising DB's namespace, here is an updated 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;
    }
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM