Уникальный случай «Вызов функции-члена set_type () на ноль» - PullRequest
0 голосов
/ 18 октября 2018

Привет, ребята, я прошел все 25 вопросов, связанных с названием моего вопроса, и я уверен, что мой сценарий совершенно другой.

Вот ошибка

 Fatal error: Uncaught Error: Call to a member function set_type() on null in /path_to_file/login.php:55 Stack trace: #0 /path_to_call_page/login.php(24): login->authenticate() #1 {main} thrown in /path_to_file/login.php on line 55

Вот мой код

<?php

class login
 {
public $username;
public $password;
public $err;
public $table_name;
public $session_data;
public $md5 = true;
public $username_column = "username";
public $password_column = "password";
public $builder;

function _construct($username,$password)
{
    $this->username = $username;
    $this->password = $password;
    $this->builder = new queryBuilder();
}

function set_table($tablename)
{
    $this->table_name = $tablename;
}

/*
 * Tells the login class where to find the username and password in database table
 */
function set_columns($username_col,$password_col)
{
    $this->username_column = $username_col;
    $this->password_column = $password_col;
}

function md5_on()
{
    $this->md5 = true;
}

function md5_off()
{
    $this->md5 = false;
}

function authenticate()
{
    $db = new mySQLConnection();
    $db->select();
    // if md5 is turned on
    if($this->md5)
        $this->password = md5($this->password);


    $this->builder->set_type("SELECT");
    $this->builder->set_table_name($this->table_name);
    $this->builder->set_where("WHERE $this->username_column = '$this->username' and $this->password_column = '$this->password'");


    $query = $this->builder->build_query();
    if($db->execute_query($query))
        $data = $db->fetch($db->result);
    else
    {
        die($db->error);
    }
    if($db->rows_affected() == 1)
    {
        $this->session_data = $data[0];
        return true;
    }
    else
    {
        $this->err = "Invalid username or password. Please try again";
        return false;
    }
}

function get_error()
{
    return $this->err;
}

}

?>

Ошибка возникает везде, где у меня есть $ this-> builder, и я определил его в методе _construct.

Это класс queryBuilder

 class queryBuilder
 {
    var $data;
    var $field;
    var $tableName;

    var $databaseName;

    var $where;
    var $order;
    var $group;
    var $limit;
    var $queryString;
    var $error;


    private function put_quotes1($field)
    {
            $field = trim($field);
            $field = "`".$field."`";
            return $field;
    }

    private function put_quotes2($field)
    {
            $field = trim($field);
            $field = "'".$field."'";
            return $field;
    }

    function set_type($type)
    {
            $this->type = $type;
    }

    function set_data($data)
    {
            $this->data = $data;
    }

    function set_field($field = null)
    {
            $this->field = $field;
    }

    function set_where($where)
    {
            $this->where = $where;
    }

    function set_limit($limit)
    {
            $this->limit = $limit;
    }

    function set_order($order)
    {
            $this->order = $order;
    }

    function set_table_name($name)
    {
        $this->tableName = $name;
    }

    function prepare_data($data)
    {
            if(is_array($data))
            {
                    foreach($data as $k => $v)
                    {
                            $this->field[] = $k; //setting the column names
                            $this->data[] = $v; // setting the values
                    }
            }

    }

    function build_query()
    {
         switch($this->type)
         {
            case 'SHOW':
                            $database_name = $this->put_quotes1($this->databaseName);

                            $this->queryString = "SHOW ";
                            if(!isset($this->field) || is_null($this->field))
                                    $this->queryString .= "DATABASES LIKE 'thirdeye%'; ";
                            else{
                                    $noFields = count($this->field); //no of fields in table
                                    for($i = 0; $i < $noFields; $i++)
                                    {
                                            if($i == ($noFields- 1))  // if on the last field
                                                    $this->queryString .= $this->put_quotes1($this->field[$i]).' ';
                                            else
                                                    $this->queryString .= $this->put_quotes1($this->field[$i]).',';
                                    }
                                }


                          break;


            case 'INSERT':
                            $table_name = $this->put_quotes1($this->tableName);
                            $this->queryString = "INSERT INTO ".$table_name." (";
                            $noFields = count($this->field);
                            $noData = count($this->data);

                            if($noFields > 0 && $noData > 0)
                            {
                                    for($i = 0; $i < $noFields; $i++)
                                    {
                                            if($i == ($noFields- 1))
                                            $this->queryString .= $this->put_quotes1($this->field[$i]).')';
                                            else
                                            $this->queryString .= $this->put_quotes1($this->field[$i]).',';
                                    }

                                    $this->queryString.= " VALUES (";


                                    for($i = 0; $i < $noData; $i++)
                                    {
                                            if($i == ($noData -1))
                                            $this->queryString .= $this->put_quotes2($this->data[$i]).');';
                                            else
                                            $this->queryString .= $this->put_quotes2($this->data[$i]).',';
                                    }

                            }
                            else
                            {
                                    $this->error = "No column name or data was supplied";
                            }

                            break;

            case 'SELECT':
                            $table_name = $this->put_quotes1($this->tableName);

                            $this->queryString = "SELECT ";
                            if(!isset($this->field) || is_null($this->field))
                                    $this->queryString .= "* ";
                            else{
                                    $noFields = count($this->field); //no of fields in table
                                    for($i = 0; $i < $noFields; $i++)
                                    {
                                            if($i == ($noFields- 1))  // if on the last field
                                                    $this->queryString .= $this->put_quotes1($this->field[$i]).' ';
                                            else
                                                    $this->queryString .= $this->put_quotes1($this->field[$i]).',';
                                    }
                                }

                            $this->queryString .= "FROM ".$table_name;

                            if(isset($this->where))
                                    $this->queryString .= " ".$this->where;

                            if(isset($this->order))
                            $this->queryString .= " ".$this->order;

                            if(isset($this->limit))
                            $this->queryString .= " ".$this->limit;

                            else
                                    $this->queryString .= ";";

                          break;


            case 'UPDATE':

                            $table_name = $this->put_quotes1($this->tableName);

                            $this->queryString = "UPDATE ". $table_name. " SET ";


                            $noFields = count($this->field); //no of fields in table

                            if(is_array($this->field) && is_array($this->data) && isset($this->where))
                            {
                                    for($i = 0; $i < $noFields; $i++)
                                    {
                                            if($i == ($noFields -1))
                                            $this->queryString .= $this->put_quotes1($this->field[$i])." = ". $this->put_quotes2($this->data[$i]).' ';
                                            else
                                            $this->queryString .= $this->put_quotes1($this->field[$i])." = ". $this->put_quotes2($this->data[$i]).',';

                                    }

                                    $this->queryString .= " ".$this->where.";";
                            }
                            else
                            {
                                    $this->error = "Cannot build query. One of the following was not set";
                            }

                            break;


            case 'DELETE':
                            $table_name = $this->put_quotes1($this->tableName);

                            $this->queryString = "DELETE FROM ".$table_name;

                            if(isset($this->where))
                            {
                                    $this->queryString .= " ".$this->where.";";
                            }
                            else
                            {
                                    $this->error = "Connot build. No condition was set";
                            }
                            break;
      }
   return $this->queryString;
   }

  }

Любые указатели помогут.Помните, что я уже читал предыдущие вопросы, поэтому предложенный вариант редактирования или кодовый ответ был бы отличным.

...