繁体   English   中英

无法从php中的类访问另一个类的方法

[英]Can not access to the method of another class from a class in php

我有这样的数据库类

class DB {
    private static $_instance = null;
    private $_pdo ,
            $_query ,
            $_results, 
            $_error=false, 
            $_count=0;

    private function __construct() {
        try {
            $this->_pdo= new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
            //echo 'connected';

        } catch (PDOException $e) {
            die($e->getMessage());
        }
        }

        public static function getInstance(){
            if (!isset(self::$_instance)) {
                self::$_instance = new DB();
            }
            return self::$_instance;
        }
         public function get($table,$where){
            return $this->action("select *",$table,$where);
        }

}

我还有另一个需要使用DB类的类

class Validate {
    private $_passed = false,
            $_errors = array(),
            $_db     = null;

    public function _construct (){

        $this->_db = DB::getInstance();
    }

   public function check($source , $items = array()){
            $check = $this->_db->get($rule_value,array($item, '=',$value)); 
  }

我的问题是,当我运行该程序时,出现了这样的错误

在非对象上调用成员函数get()

由于我的构造函数中已经有DB类的实例,因此导致此错误的原因是什么?

您的Validate构造函数定义不正确,因此没有被调用。

public function _construct (){

没有构造函数调用意味着$this->_db在您的check()方法中返回null 它应该简单地再加上一个下划线:

public function __construct() {
                ^

PHP中的构造函数和析构函数

暂无
暂无

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

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