2015-12-11 43 views
-1

我對OOP相當陌生,這是我第一次嘗試寫OOP代碼。PHP OOP - 打電話給非會員func

出於某種原因,它不斷給我

「調用一個成員函數 [路徑/到/文件] log.php

我的execute()一個非對象在log.php以下代碼

function logMessage($logtype = 0, $logmessage = '') 
{ 
    global $config, $getSQL, $sql, $queryBuilder; 

    if ($config['log_threshold'] >= $logtype) 
    { 
     // Check database connection 
     if(($sql instanceof MySQLi) == false) { 
      return array('status' => false, 'type' => 1, 'message' => 'MySQL connection is invalid'); 
     } 

     // Check type 
     if($logtype == '' || $logtype === NULL) { 
      return array('status' => false, 'type' => 1, 'message' => 'No type specified'); 
     } 

     // Check message 
     if($logmessage == '') { 
      return array('status' => false, 'type' => 1, 'message' => 'Message is empty'); 
     } 

     // Get IP address 
     if(($remote_addr = $_SERVER['REMOTE_ADDR']) == '') { 
      $remote_addr = "REMOTE_ADDR_UNKNOWN"; 
     } 

     // Get requested script 
     if(($request_uri = $_SERVER['REQUEST_URI']) == '') { 
      $request_uri = "REQUEST_URI_UNKNOWN"; 
     } 

     if ($getSQL) 
     { 
      // Escape values 
      $logtype  = $sql->escape_string($logtype); 
      $logmessage = $sql->escape_string($logmessage); 
      $remote_addr = $sql->escape_string($remote_addr); 
      $request_uri = $sql->escape_string($request_uri); 
     } 

     // Construct query 
     $insert = array('site_log'); 
     $columns = array('remote_addr', 'request_uri', 'log_type', 'message'); 
     $values = array('$remote_addr', '$request_uri', '$logtype', '$logmessage'); 
     $result = $queryBuilder->execute(1, $insert, $columns, $values); 

     if($result) { 
      return array('status' => true); 
     } 
     else { 
      return array('status' => false, 'type' => 1, 'message' => 'Unable to write to the database'); 
     } 
    } 
} 

類的QueryBuilder是在的common.php首先被包括在內,則$ QueryBuilder的運行和啓動的QueryBuilder類;

class QueryBuilder 
{ 
    protected $db; 
    protected $sql; 

    public function __construct($db, $sql) 
    { 
     $this->dbPrefix = $db['dbprefix']; 
     $this->database = $sql; 
    } 

    public function select($column = '', $column2 = '', $column3 = '', $column4 = '', $column5 = '', $column6 = '') 
    { 
     !$column == '' ? $column = '%PARAM%'.$column.'%PARAM2%' : ''; 
     !$column2 == '' ? $column2 = '%PARAM3%'.$column2.'%PARAM2%' : ''; 
     !$column3 == '' ? $column3 = '%PARAM3%'.$column3.'%PARAM2%' : '';   !$column4 == '' ? $column4 = '%PARAM3%'.$column4.'%PARAM2%' : '';   !$column5 == '' ? $column5 = '%PARAM3%'.$column5.'%PARAM2%' : '';   !$column6 == '' ? $column6 = '%PARAM3%'.$column6.'%PARAM2%' : ''; 

     $search = array('{', '[', '(', ',', "\\", '/', ';', '&', ')', ']', '}'); 
     $replace = array('', '', '', '', '', '', '', '', '', '', ''); 
     $output = array($column, $column2, $column3, $column4, $column5, $column6); 
     $output = str_replace($search, $replace, $output); 
     $search2 = array('%PARAM%', '%PARAM2%', '%PARAM3%'); 
     $replace2 = array(' \'', ', \'', '\''); 
     $output = str_replace($search2, $replace2, $output); 
     $output = 'SELECT'.$output; 
     return $output; 
    } 

    public function from($table = '', $table2 = '', $table3 = '', $table4 = '', $table5 = '', $table6 = '') 
    { 
     !$table == '' ? $table = '%PARAM%'.$table.'%PARAM2%' : ''; 
     !$table2 == '' ? $table2 = '%PARAM3%'.$table2.'%PARAM2%' : ''; 
     !$table3 == '' ? $table3 = '%PARAM3%'.$table3.'%PARAM2%' : ''; 
     !$table4 == '' ? $table4 = '%PARAM3%'.$table4.'%PARAM2%' : ''; 
     !$table5 == '' ? $table5 = '%PARAM3%'.$table5.'%PARAM2%' : ''; 
     !$table6 == '' ? $table6 = '%PARAM3%'.$table6.'%PARAM2%' : ''; 

     $search = array('{', '[', '(', ',', "\\", '/', ';', '&', ')', ']', '}'); 
     $replace = array('', '', '', '', '', '', '', '', '', '', ''); 
     $output = array($table, $table2, $table3, $table4, $table5, $table6); 
     $output = str_replace($search, $replace, $output); 
     $search2 = array('%PARAM%', '%PARAM2%', '%PARAM3%'); 
     $replace2 = array(' \''.$dbPrefix, ', \'', '\''.$dbPrefix); 
     $output = str_replace($search2, $replace2, $output); 
     $output = 'FROM'.$output; 
     return $output; 
    } 

    public function insert($table = '', $table2 = '', $table3 = '', $table4 = '', $table5 = '', $table6 = '') 
    { 
     !$table == '' ? $table = '%PARAM%'.$table.'%PARAM2%' : ''; 
     !$table2 == '' ? $table2 = '%PARAM3%'.$table2.'%PARAM2%' : ''; 
     !$table3 == '' ? $table3 = '%PARAM3%'.$table3.'%PARAM2%' : ''; 
     !$table4 == '' ? $table4 = '%PARAM3%'.$table4.'%PARAM2%' : ''; 
     !$table5 == '' ? $table5 = '%PARAM3%'.$table5.'%PARAM2%' : ''; 
     !$table6 == '' ? $table6 = '%PARAM3%'.$table6.'%PARAM2%' : ''; 

     $search = array('{', '[', '(', ',', "\\", '/', ';', '&', ')', ']', '}'); 
     $replace = array('', '', '', '', '', '', '', '', '', '', ''); 
     $output = array($table, $table2, $table3, $table4, $table5, $table6); 
     $output = str_replace($search, $replace, $output); 
     $search2 = array('%PARAM%', '%PARAM2%', '%PARAM3%'); 
     $replace2 = array('\''.$dbPrefix, ', \'', '\''.$dbPrefix); 
     $output = str_replace($search2, $replace2, $output); 
     $output = 'INSERT INTO '.$output; 
     return $output; 
    } 

    public function insert_columns($column = '', $column2 = '', $column3 = '', $column4 = '', $column5 = '', $column6 = '') 
    { 
     !$column == '' ? $column = '%PARAM%'.$column.'%PARAM2%' : ''; 
     !$column2 == '' ? $column2 = '%PARAM3%'.$column2.'%PARAM2%' : ''; 
     !$column3 == '' ? $column3 = '%PARAM3%'.$column3.'%PARAM2%' : ''; 
     !$column4 == '' ? $column4 = '%PARAM3%'.$column4.'%PARAM2%' : ''; 
     !$column5 == '' ? $column5 = '%PARAM3%'.$column5.'%PARAM2%' : ''; 
     !$column6 == '' ? $column6 = '%PARAM3%'.$column6.'%PARAM2%' : ''; 

     $search = array('{', '[', '(', ',', "\\", '/', ';', '&', ')', ']', '}'); 
     $replace = array('', '', '', '', '', '', '', '', '', '', ''); 
     $output = array($column, $column2, $column3, $column4, $column5, $column6); 
     $output = str_replace($search, $replace, $output); 
     $search2 = array('%PARAM%', '%PARAM2%', '%PARAM3%'); 
     $replace2 = array('\'', ', \'', '\''); 
     $output = str_replace($search2, $replace2, $output); 
     $output = ' ('.$output.') '; 
     return $output; 
    } 

    public function values($table = '', $table2 = '', $table3 = '', $table4 = '', $table5 = '', $table6 = '') 
    { 
     !$table == '' ? $table = '%PARAM%'.$table.'%PARAM2%' : ''; 
     !$table2 == '' ? $table2 = '%PARAM3%'.$table2.'%PARAM2%' : ''; 
     !$table3 == '' ? $table3 = '%PARAM3%'.$table3.'%PARAM2%' : ''; 
     !$table4 == '' ? $table4 = '%PARAM3%'.$table4.'%PARAM2%' : ''; 
     !$table5 == '' ? $table5 = '%PARAM3%'.$table5.'%PARAM2%' : ''; 
     !$table6 == '' ? $table6 = '%PARAM3%'.$table6.'%PARAM2%' : ''; 

     $search = array('{', '[', '(', ',', "\\", '/', ';', '&', ')', ']', '}'); 
     $replace = array('', '', '', '', '', '', '', '', '', '', ''); 
     $output = array($table, $table2, $table3, $table4, $table5, $table6); 
     $output = str_replace($search, $replace, $output); 
     $search2 = array('%PARAM%', '%PARAM2%', '%PARAM3%'); 
     $replace2 = array('\''.$dbPrefix, ', \'', '\''.$dbPrefix); 
     $output = str_replace($search2, $replace2, $output); 
     $output = 'VALUES ('.$output.');'; 
     return $output; 
    } 

    public function execute($type, $tables, $columns, $values) 
    { 
     if ($type == 1) 
     { 
      $tables = $this->insert($tables); 
      $columns = $this->insert_columns($columns); 
      $values = $this->values($values); 
      $result = $this->database->mysqli_query($tables, $columns, $values); 
     } 

     if ($result) 
     { 
      $result = 'lol'; 
     } 
     else 
     { 
      $result = 'lolwat'; 
     } 
     return $result; 
    } 
} 
$queryBuilder = new QueryBuilder($db, $sql); 

$ db是爲分貝連接,其中作爲$ SQL是mysqli的連接對象的所有設置的陣列。

我知道SQL方法是不安全的,我需要添加mysqli_real_escape_string,更多的過濾器等,但我仍然在寫它和測試,所以我不會那樣做,直到它實際工作。

我很確定所需的結果是自我解釋的,如果不是那麼結果應該執行查詢,並且在這種情況下查詢日誌到數據庫,工作如下;

帶有2個參數的調用函數,參數1是嚴重性,參數2是消息。

實施例:

logMessage(3, 'Application Initialized'); 

測井完美地工作時,它使用的是普通的查詢,而不是OOP類。

回答

0

當您執行logMessage()時,您的變量$queryBuilder不存在(或不包含對象)。您應該避免使用globals,而應將$queryBuilder作爲參數傳遞給logMessage()函數。

+0

...或將logMessage()包裝到類中,並使用setter。 – syck