tags:

views:

166

answers:

4

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.

A: 

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.

Pekka
+1  A: 

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

Jani Hartikainen
+2  A: 

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

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

Dereleased
Thanks I was newing up the default PDO, since I was saying \PDO. I added a few lines of code to fix the problem while still using the singleton pattern. Just added a call to the constructor from my setup method and added an object that I can call.
adaykin
Seems like that ought to work nicely as well =)
Dereleased
A: 

Rename your PDO class to something else like PDOImplementation and try again.

mst