简体   繁体   中英

PHP can't find a function in my class

I have a class that is getting called and for some reason one of the functions I have is being found, but the other one is not. Here is my code:

<?php


namespace LMVC\Database;

class PDO
{
private static $instance = NULL;

public static function setup($dsn, $username, $password)
{
    try {
        self::$instance = new \PDO($dsn, $username, $password);
    }
    catch(PDOException $e) {
        throw new Exception("Error: " . $e->getMessage());
    }
}

public static function getInstance()
{
    if(self::$instance === NULL) {
        throw new Exception("Database is not instantiated");
    }

    return self::$instance;
}

public function query($sql)
{
    if(self::$instance === NULL) {
        throw new Exception("Database is not instantiated");
    }

    try {
        return self::$instance->query($sql);
    }
    catch(PDOException $e) {
        throw new Exception("Error: " . $e->getMessage());
    }
}

public function fetchAll($stmt)
{
    if(self::$instance === NULL) {
        throw new Exception("Database is not instantiated");
    }

    try {
        return self::$instance->fetchAll($stmt);
    }
    catch(PDOException $e) {
        throw new Exception("Error: " . $e->getMessage());
    }
}

public function prepare()
{

}

public function quote()
{

}
}

All of the functions except fetchAll are able to be found, but fetchAll is not. I can't seem to add functions that exist either. When I change the name of fetchAll to something else it can't find the functoin I rename it to. Also when I get rid of the functions that are able to be found like quote, query, and prepare, php is still able to find them, I tested using var_dump with function exists. I have tried restarting apache, restarting windows 7. I am using php 5.3.1.

Any other ideas on how to debug? I can't seem to find out how to get the error log to work by editing my php.ini file either.

You are probably implementing PHP's prebuilt PDO Object . Have you tried instantiating your object directly with the namespace path? eg

$foo = new LMVC\Database\PDO;

Otherwise, PHP may be instantiating its own object named PDO, which is in the root namespace.

EDIT FOR MORE INFO

If you are including the file that defines this class, and you do not specify the namespace in the file which instantiates this class, then PHP will only consider the declared namespace to be in effect during the execution of the file which defines said class. Outside of that include file, it will use namespace fallback to declare the global object.

You should, since PDO is an existing global object, rename your class (myPDO, perhaps?) in order to avoid this sort of confusion in the future.

Have you tried renaming the class? It could be your code is actually trying to use PHP's builtin PDO object.

I haven't worked with namespaces yet, but I'll bet a beer that you are initializing the normal PDO object and not the one you define in your namespace.

Are you setting up the right namespace when initializing your class?

Remember that namespace statements are valid for the current file only.

将您的PDO类重命名为PDOImplementation之类的名称,然后重试。

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