当不在对象上下文中时使用$ this吗?

安德洛(Andrello)

错误信息:

致命错误:不在第51行的class.db.php中的对象上下文中使用$ this

错误行:

            return $this->PDOInstance->prepare($sql, $driver_options);

代码:

class DB {   
        public $error = true; 

        private $PDOInstance = null;

        private static $instance = null;

        private function __construct()
          {
            try {

                $this->PDOInstance = new PDO('mysql:host='.HOST.';dbname='.DBNAME.';',
                                                    USER,
                                                    PASSWORD,
                                                    array(
                                                            PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
                                                            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
                                                            ));

                $this->PDOInstance->query("SET NAMES 'cp1251'");
            } 
            catch(PDOException $e) { 
                echo "error";
                exit();
            }
          }


        public static function getInstance()
        {  
            if(is_null(self::$instance))
            {
              self::$instance = new DB();
            }
            return self::$instance;
        }


        private function __clone() {
        }

        private function __wakeup() {
        }         

        public static function prepare($sql, $driver_options=array())
        {
            try {
                return $this->PDOInstance->prepare($sql, $driver_options);  /// ERROR in this line
            } 
            catch(PDOException $e) { 
                $this->error($e->getMessage());
            }
        }

         }
php_nub_qq

您正在使用$this静态函数。$this引用一个您没有的实例,该实例调用静态函数,因此发生错误。我不明白为什么在单例类中需要非静态属性,但是如果您坚持要拥有它们,这就是您可以做的

catch(PDOException $e) { 
    self::$instance->error = $e->getMessage();     
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

错误:不在对象上下文中时使用$ this

来自分类Dev

ATK4:错误:不在对象上下文中时使用$ this

来自分类Dev

不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12

来自分类Dev

PHP $ this不在对象上下文中时,用于从类外设置公共变量

来自分类Dev

在Core Data中执行子级提取时,在父级上下文中修改托管对象会传播到子级上下文吗?

来自分类Dev

不在对象上下文中使用$ this时,请使用ReflectionFunction-> invoke()

来自分类Dev

在不在对象上下文中时使用$ this时出错

来自分类Dev

不在对象上下文中使用$ this php

来自分类Dev

Zend Framework 2:致命错误-不在对象上下文中时使用$ this

来自分类Dev

如果不在对象上下文中而不使用静态方法,则使用$ this

来自分类Dev

未捕获的错误:使用__callStatic时不在对象上下文中时使用$ this

来自分类Dev

非constexpr变量有时可在constexpr上下文中使用吗?

来自分类Dev

错误:不在对象上下文中时使用“ $ this”

来自分类Dev

不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12

来自分类Dev

致命错误:不在对象上下文中时使用$ this ...在第7行

来自分类Dev

Silex / phpunit案例中出现“在不在对象上下文中时使用$ this”

来自分类Dev

造成此原因的原因:不在对象上下文中时使用$ this

来自分类Dev

致命错误:不在对象上下文中时使用$ this

来自分类Dev

当不在对象上下文中时使用$ this吗?的PHP

来自分类Dev

不在对象上下文中使用$ this php

来自分类Dev

PHP:“致命错误:不在对象上下文中时使用$ this”

来自分类Dev

当不在对象上下文中时,在同一个类中使用$ this

来自分类Dev

不在对象上下文中时使用$ this在静态方法中调用简单方法

来自分类Dev

在Codeigniter中不在对象上下文中时使用$ this

来自分类Dev

Codeigniter模型致命错误:不在对象上下文中时使用$ this

来自分类Dev

OOP致命错误:不在对象上下文中时使用$ this

来自分类Dev

PHP OOP-未捕获的错误:不在对象上下文中时使用$ this

来自分类Dev

尝试使用 Mysqli 插入时,我收到“不在对象上下文中时使用 $this”

来自分类Dev

PHP Slim 3 Framework - 在自定义类中使用 MonoLog - 不在对象上下文中时使用 $this

Related 相关文章

  1. 1

    错误:不在对象上下文中时使用$ this

  2. 2

    ATK4:错误:不在对象上下文中时使用$ this

  3. 3

    不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12

  4. 4

    PHP $ this不在对象上下文中时,用于从类外设置公共变量

  5. 5

    在Core Data中执行子级提取时,在父级上下文中修改托管对象会传播到子级上下文吗?

  6. 6

    不在对象上下文中使用$ this时,请使用ReflectionFunction-> invoke()

  7. 7

    在不在对象上下文中时使用$ this时出错

  8. 8

    不在对象上下文中使用$ this php

  9. 9

    Zend Framework 2:致命错误-不在对象上下文中时使用$ this

  10. 10

    如果不在对象上下文中而不使用静态方法,则使用$ this

  11. 11

    未捕获的错误:使用__callStatic时不在对象上下文中时使用$ this

  12. 12

    非constexpr变量有时可在constexpr上下文中使用吗?

  13. 13

    错误:不在对象上下文中时使用“ $ this”

  14. 14

    不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12

  15. 15

    致命错误:不在对象上下文中时使用$ this ...在第7行

  16. 16

    Silex / phpunit案例中出现“在不在对象上下文中时使用$ this”

  17. 17

    造成此原因的原因:不在对象上下文中时使用$ this

  18. 18

    致命错误:不在对象上下文中时使用$ this

  19. 19

    当不在对象上下文中时使用$ this吗?的PHP

  20. 20

    不在对象上下文中使用$ this php

  21. 21

    PHP:“致命错误:不在对象上下文中时使用$ this”

  22. 22

    当不在对象上下文中时,在同一个类中使用$ this

  23. 23

    不在对象上下文中时使用$ this在静态方法中调用简单方法

  24. 24

    在Codeigniter中不在对象上下文中时使用$ this

  25. 25

    Codeigniter模型致命错误:不在对象上下文中时使用$ this

  26. 26

    OOP致命错误:不在对象上下文中时使用$ this

  27. 27

    PHP OOP-未捕获的错误:不在对象上下文中时使用$ this

  28. 28

    尝试使用 Mysqli 插入时,我收到“不在对象上下文中时使用 $this”

  29. 29

    PHP Slim 3 Framework - 在自定义类中使用 MonoLog - 不在对象上下文中时使用 $this

热门标签

归档