简体   繁体   中英

how to “install” class inside class::static var

i'm trying to install class inside class, in static method

class db extends PDO
    {
        private static $error;
        private static $sql;
        private static $bind;
        private static $errorCallbackFunction;
        private static $errorMsgFormat;
        private static $pdo;

        public function __construct($dsn = "mysql:host=localhost;dbname=main;",
                                        $user="root", $passwd="")
        {
            $options = array(
                PDO::ATTR_PERSISTENT => true, 
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
            );

            try {
                self::$pdo = new PDO($dsn, $user, $passwd, $options);
            } catch (PDOException $e) {
                self::$error = $e->getMessage();
            }
        }

this is how i install a class inside the private static $pdo, i think it can be done, but when i calling

public static function run($sql, $bind="")
{
    self::$sql = trim($sql);
    self::$bind = self::cleanup($bind);
    self::$error = "";

    try {
        $pdostmt = self::$pdo->prepare(self::$sql);

there is an error with the error message

Call to a member function prepare() on a non-object

isit possible to install class inside a static variable?thanks for helping.

That error means the self::$pdo is not an object. These are all static variables and methods, yet you rely on the instance constructor to set the PDO instance, so if someone calls db::run() before calling new db(...) , the class will not operate correctly. If you want to continue with the class being all-static, you need to somewhere do the setup done in the constructor before calling ::run .

I would suggest you try using the class as an instance, however, like so:

class DB {
    private $error;
    private $sql;
    private $bind;
    private $errorCallbackFunction;
    private $errorMsgFormat;
    private $pdo;

    public function __construct($dsn = "mysql:host=localhost;dbname=main;", $user="root", $passwd=""){
       $options = array(
           PDO::ATTR_PERSISTENT => true, 
           PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
           PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
       );

       try {
           $this->pdo = new PDO($dsn, $user, $passwd, $options);
       } catch (PDOException $e) {
           $this->error = $e->getMessage();
       }
   }

   public function run($sql, $bind = ''){
       $this->sql = trim($sql);
       $this->bind = self::cleanup($bind);
       $this->error = "";

       try {
           if(!isset($this->pdo)){
               throw new \RuntimeException('No PDO instance');
           }
           $pdostmt = $this->pdo->prepare(self::$sql);
       ...
    }
}

You would need to keep track of the instance of that class, but this way you don't have to worry about being in an un-constructed/instantiated state.

Also, in your example, you are extending the base PDO class. Extending internal PHP classes is usually a dangerous practice, and given that you are building what looks like a wrapper rather than an extension of the PDO class, there doesn't look like any reason to do that.

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