简体   繁体   English

PHP在我的课程中找不到函数

[英]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. 可以找到除fetchAll以外的所有功能,但找不到fetchAll。 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. 当我将fetchAll的名称更改为其他名称时,找不到将其重命名为的functoin。 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. 另外,当我摆脱报价,查询和准备等能够找到的功能时,php仍然能够找到它们,我使用var_dump与功能存在进行了测试。 I have tried restarting apache, restarting windows 7. I am using php 5.3.1. 我尝试重新启动apache,重新启动Windows7。我正在使用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. 我似乎也无法找到如何通过编辑php.ini文件来使错误日志正常工作的方法。

You are probably implementing PHP's prebuilt PDO Object . 您可能正在实现PHP的预构建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. 否则,PHP可能会实例化其自己的名为PDO的对象,该对象位于根名称空间中。

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. 如果您包含定义此类的文件,并且未在实例化此类的文件中指定名称空间,则PHP仅会在定义该类的文件执行期间将声明的名称空间视为有效。 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. 由于PDO是现有的全局对象,因此应该重命名您的类(也许是myPDO?),以避免将来出现此类混乱。

Have you tried renaming the class? 您是否尝试过重命名课程? It could be your code is actually trying to use PHP's builtin PDO object. 可能是您的代码实际上正在尝试使用PHP的内置PDO对象。

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. 我还没有使用名称空间,但是我敢打赌,您是在初始化普通的PDO对象,而不是您在名称空间中定义的对象。

Are you setting up the right namespace when initializing your class? 在初始化类时是否设置了正确的名称空间?

Remember that namespace statements are valid for the current file only. 请记住,名称空间语句仅对当前文件有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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