Mithun Sarker
Mithun Sarker

Reputation: 4023

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

I have class for database which is like this

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);
        }

}

And I have another class from where I need to use the DB class

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)); 
  }

My problem is when I run this program I got an error like this

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

What is the reason for this error as I already have an instance of DB class in my constructor .

Upvotes: 0

Views: 26

Answers (1)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107526

Your Validate constructor isn't defined correctly, and thus isn't being called.

public function _construct (){

No constructor call means $this->_db is returning null in your check() method. It should simply have another underscore:

public function __construct() {
                ^

Constructors and Destructors in PHP

Upvotes: 1

Related Questions