无法访问扩展数据库类中的公共方法

时间:2018-09-02 08:22:15

标签: php oop

我有一个数据库班。所有其他类都将其用作扩展器。但是,当我想从扩展类方法内部运行查询方法时,它将引发此错误:

Fatal error: Uncaught Error: Class name must be a valid object or a string in /home/ubuntu/workspace/app/logics/Classes/Class.DB.php:623
Stack trace:
#0 /home/ubuntu/workspace/app/logics/Classes/Class.DB.php(483): SafeMySQL->error('SafeMySQL: . Fu...')
#1 /home/ubuntu/workspace/app/logics/Classes/Class.DB.php(233): SafeMySQL->rawQuery('SELECT userpass...')
#2 /home/ubuntu/workspace/app/logics/Classes/Class.Users.php(114): SafeMySQL->getRow('SELECT userpass...', 'h.elvin@ya.ru')
#3 /home/ubuntu/workspace/app/logics/Classes/Class.Users.php(90): User->verify_password()
#4 /home/ubuntu/workspace/app/index.php(26): User->login(Array)
#5 /home/ubuntu/workspace/app/logics/Router/Router.php(70): {closure}(Object(Request))
#6 /home/ubuntu/workspace/app/logics/Router/Router.php(75): Router->resolve()
#7 [internal function]: Router->__destruct()
#8 {main}
  thrown in /home/ubuntu/workspace/app/logics/Classes/Class.DB.php on line 623

我使用此数据库类:https://github.com/colshrapnel/safemysql

我的User类如下:

    class User extends SafeMySQL {

    private $useremail;

        // Login
        public function login($creds = array()) {

            if (is_array($creds) && !empty($creds)) {

                $this->useremail = trim($creds['useremail']);
                $this->userpassword = trim($creds['userpassword']);

                if ($this->verify_password()) {

                    session_regenerate_id(true);
                    $_SESSION['id'] = session_id();
                    $_SESSION['useremail'] = $this->useremail;
                    $_SESSION['is_logged'] = true;
                    $this->is_logged = true;

                    // Set a cookie that expires in one week
                    if (isset($_POST['remember']))
                        setcookie('useremail', $this->useremail, time() + 604800);

                    // To avoid resending the form on refreshing
                    header('Location: ' . $_SERVER['REQUEST_URI']);
                    exit();

                } else $this->error[] = 'Wrong user or password.';

            }

        }

        // Check if password match
        private function verify_password() {
            $get_pass = $this->getRow('SELECT userpassword FROM Users WHERE useremail=?s', $this->useremail);
            return password_verify($this->password, $get_pass[0]['userpassword']);

        }
}

verify_password方法执行代码时,出现以上错误消息。

2 个答案:

答案 0 :(得分:0)

您正在使用的库中似乎有一个错误,就在这里: https://github.com/colshrapnel/safemysql/blob/master/safemysql.class.php#L623

new $this周围应该有括号(顺便说一句,我什至不知道有效,我相信new self / new static会更干净,但不是重点)。

无论如何,要实例化User实例,要解决您的问题,请尝试这样做:

$user = new User(['errmode' => 'error']);

这应该触发错误而不是引发异常,并让您在查询中看到实际的错误(因为似乎有一个错误)。

否则,您可以自己修复该lib中的错误(添加括号)和/或创建github问题以通知其创建者。

答案 1 :(得分:-1)

我完全注意到这是否可以解决您的问题,但是...

您的数据库类中的if (!$res) 方法似乎检查不正确。在第474行上说:

$res

其中mysqli_query()包含if ($res === FALSE) 的结果。在手册中说明了这种结果:

  

失败时返回FALSE。要成功执行SELECT,SHOW,DESCRIBE或   EXPLAIN查询mysqli_query()将返回mysqli_result对象。对于   其他成功的查询mysqli_query()将返回TRUE。

请参阅:http://nl1.php.net/manual/en/mysqli.query.php

所以我认为支票应该是:

class BinarySearchTree:
    def __init__(self):
        self.info = None
        self.left = None
        self.right = None

    def insert(self,val):
        if self.info is None:
            self.info = val
            self.left = BinarySearchTree()
            self.right = BinarySearchTree()
        elif self.info > val:
            self.left.insert(val)
        elif self.info < val:
            self.right.insert(val)

在手册中的某些注释中使用。