2011-12-15 37 views
5

Nhận một lỗi rất lạ ở đây, tôi đang viết một lớp cơ sở dữ liệu dạng phẳng và tất cả đều hoạt động tốt cho đến khi tôi làm mới và bây giờ tôi liên tục nhận được thông báo này:PHP: Lỗi nghiêm trọng: Gọi tới chức năng thành viên trên một đối tượng không phải là đối tượng

Fatal error: Call to a member function name() on a non-object in /home/reithg/public_html/test/engine/class.database.php on line 50

tôi kêu gọi các lớp như sau:

<?php 
ini_set('display_errors', '1'); 

require('engine/class.database.php'); 

$config = new Config("lessons", array('first', 'second', 'third', 'fourth'), "data/"); 
$db = new Database($config, true); 

print("Querying DB for 'theta' no exclusions: <br />"); 
print_r($db->query('theta', NULL, NULL)); 

print("<p /> Querying DB for 'theta' in column 'second': <br />"); 
print_r($db->query('theta', 'second', NULL)); 

print("<p /> Querying DB for first two rows: <br />"); 
print_r($db->getRows(2)); 

print("<p /> Querying DB for last three rows: <br />"); 
print_r($db->getRows(3, true)); 

print("<p /> Cleaning data for safe DB input: <br />"); 
$testInput = array('escape|these||delimiters','and\these\\slashes','and\0these\0nulls',"don't, forget quotes"); 
print("input: "); 
print_r($testInput); 
echo("<br />output: "); 
print($db->addRow($testInput)); 
?> 

đây là class.database.php tôi

<?php 
require('class.config.php'); 
require('class.column.php'); 

    class Database { 
     private 
      $_config, 
      $_pointer; 

     public function __construct(Config $config) { 
      $this->_config = $config; 
      return true; 
     } 

     private function connect($method) { 
      if (!($this->_pointer = @fopen($this->_config->db(), $method))) 
      echo("Unable to connect to database"); 
     } 

     private function disconnect() { 
      fclose($this->_pointer); 
     } 

     private function lock($method) { 
      if(flock($this->_pointer, $method)) 
       return true; 
      return false; 
     } 

     private function unlock() { 
      flock($this->_pointer, LOCK_UN); 
     } 

     private function cleanInput($input) { 
      $data = array_map(array($this, 'escapeData'), $input); 
      $output = implode($this->_config->delimiter(), $data)."\r\n"; 
      return $output; 
     } 

     private function escapeData($data) 
     { 
      $search = array('\\', '"', "'", '\\0', '\n', $this->_config->delimiter()); 
      $replace = array('\\\\', '\"', "\'", '\\0', '\\n', '\\'.$this->_config->delimiter()); 
      $output = str_replace(array_unique($search), array_unique($replace), $data); 
      return $output; 
     } 

     private function formatRow($data) { 
      foreach($data as $key => $value) { 
       $row[$this->_config->columns($key, "position")->name()] = $value; 
      } 
      return $row; 
     } 

     public function dumpToArray() { 
      $arrayDump; 
      foreach(file($this->_config->db(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $row => $content) 
       $arrayDump[$row] = formatRow(explode($this->_config->delimiter(),$content)); 
      return $arrayDump; 
     } 

     public function addRow(array $data) { 
      $this->connect('ab'); 
      if($this->lock(LOCK_EX)) { 
       // fwrite($this->_pointer, $this->cleanInput($data)); 
       echo($this->cleanInput($data)); 
       $this->unlock(); 
       $this->disconnect(); 
       return true; 
      } else { 
       $this->disconnect(); 
       return false; 
      } 
     } 

     public function query($value, $column = NULL, $limit = NULL) { 
      $this->connect('rb'); 
      $results = array(); 
      while ((is_null($limit) || (count($results) < $limit)) && !feof($this->_pointer)) { 
       $data = explode($this->_config->delimiter(), fgets($this->_pointer, 1024)); 
       if(!is_null($column)) { 
        if ($data[$this->_config->columns($column, "string")->index()] == $value) 
         array_push($results, $this->formatRow($data)); 
       } else { 
        if (in_array($value, $data)) 
         array_push($results, $this->formatRow($data)); 
       } 
      } 
      $this->disconnect(); 
      switch (count($results)) { 
       case 0; 
        return false; 
       case 1; 
        return $results[0]; 
       default; 
        return $results; 
      } 
     } 

     public function getRows($limit = 1, $reverse = false) { 
      $this->connect('rb'); 
      $offset = 0; 
      $results = array(); 
      if ($reverse) { 
       while(count($results) < $limit && fseek($this->_pointer, $offset, SEEK_END) >= 0) { 
        $char = fgetc($this->_pointer); 
        if($char == "\n" || $char == "\r"){ 
         $offset --; 
         $data = explode($this->_config->delimiter(), fgets($this->_pointer, 1024)); 
         array_push($results, $this->formatRow($data)); 
        } 
        $offset--; 
       } 
       $results = array_reverse($results); 
      } else { 
       while ((($limit === NULL) || (count($results) < $limit)) && !feof($this->_pointer)) { 
        $data = explode($this->_config->delimiter(), fgets($this->_pointer, 1024)); 
        array_push($results, $this->formatRow($data)); 
       } 
      } 
      $this->disconnect(); 
      return $results; 
     } 
    } 
?> 

class.config.php

<?php 
    class Config { 
     private 
      $_db, 
      $_file, 
      $_columns = array(), 
      $_directory, 
      $_delimiter; 

     public function __construct($file, array $columns, $directory = NULL, $delimiter = "|") { 
      $this->_db = $directory.$file.".db"; 
      $this->defineColumns($columns); 
      $this->_directory = $directory; 
      $this->_delimiter = $delimiter; 
     } 

     public function db() { 
      return $this->_db; 
     } 

     public function delimiter() { 
      return $this->_delimiter; 
     }  

     private function defineColumns($constants) { 
      for ($i=0;$i<count($constants);$i++) { 
       if(in_array($constants[$i], $this->_columns)) 
        die("Column names must be unique"); 
       $column = new Column($constants[$i], $i); 
       $this->_columns[$column->name()] = $column; 
      } 
     } 

     public function columns($index, $search = "string") { 
      switch ($search) { 
       case "string"; 
        return $this->_columns[$index]; 
        break; 
       case "position"; 
        $keys = array_keys($this->_columns); 
        return $this->_columns[$keys[$index]]; 
        break; 
       default; 
        return false; 
      } 
     } 
    } 
?> 

class.column.php

<?php 
    class Column { 
     const 
      ALL = "0", 
      STRING = "1", 
      NUMBER = "2", 
      INT = "3", 
      AUTO_INCREMENT = "4", 
      CURRENT_TIME = "5"; 

     private 
      $_type = ALL, 
      $_name, 
      $_index, 
      $_maxChars = "256"; 

     public function __construct($name, $index, $type = NULL, $maxChars = NULL) { 
      $this->_name = $name; 
      $this->_index = $index; 
      if(!is_null($type)) 
       setDataType($type); 
      if(!is_null($maxChars)) 
       setMaxChars($maxChars); 
      return $this; 
     } 

     public function setDataType($type) { 
      switch ($type) { 
       case ALL; 
       case STRING; 
       case NUMBER; 
       case INT; 
       case AUTO_INCREMENT; 
       case CURRENT_TIME; 
        $this->_type = $type; 
        break; 
       default; 
        return false; 
      } 
     } 

     public function auditData($data) { 
      switch ($this->_type) { 
       case ALL; 
        $output = $data; 
        break; 
       case STRING; 
        $output = (string) $data; 
        break; 
       case NUMBER; 
        $output = (float) $data; 
        break; 
       case INT; 
        $output = (int) $data; 
        break; 
       case AUTO_INCREMENT; 
        $output = (int) $data; 
        break; 
       case CURRENT_TIME; 
        $output = time(); 
        break; 
       default; 
        return false; 
      } 
      return $output; 
     } 

     public function setMaxChars($maxChars) { 
      if(is_int($maxChars)) { 
       $this->_maxChars = $maxChars; 
      } 
     } 

     public function name() { 
      return $this->_name; 
     } 

     public function index() { 
      return $this->_index; 
     } 
    } 
?> 

Tôi biết đó là rất nhiều mã nhưng tôi không thể làm việc ra tại sao điều này xảy ra là tất cả đột nhiên, theo nghĩa đen trong một lần làm mới mà không có bất kỳ thay đổi nào đối với mã. Ngay cả khi tôi quay trở lại phiên bản trước đó cũng làm việc này đang xảy ra.

Khi tôi cố gắng để làm:

print($this->_config->columns($key, "position")); 

Nó trả về:

Catchable fatal error: Object of class Column could not be converted to string in /home/reithg/public_html/test/engine/class.database.php *on line 50*

nào cho thấy tôi đang thực hiện name() trên một thành viên của Cột lớp trong đó có một phương pháp nào gọi là name()

Khi tôi làm:

print($this->_config->columns($key, "position")->name()); 

nó trả về (một từ mỗi lần như trong vòng lặp foreach);

first second third fourth first second third fourth

Vì vậy, rõ ràng là bạn đang làm việc trước 1 dòng.

+5

Gỡ lỗi - là những gì mỗi người trong chúng ta thực hiện tối đa 90% trong ngày. Vậy bạn nên. – zerkms

+0

Khó nói, người đàn ông. Bạn nên chú ý đến dòng lỗi và tìm đối tượng '$ this -> _ config-> columns nào ($ key," position ") -> name()' không hoạt động. –

+0

@ Márcio Tôi đã nhận thấy điều này nhưng '$ this -> _ config-> cột ($ key," position ")' trả về một thành viên của lớp 'Column' có một phương thức' name() '. @zerkms Đây là một lỗi đột ngột và là do không có thay đổi tôi đã rõ ràng đã được cố gắng để gỡ lỗi nó, nhưng khi đó không hoạt động ...? –

Trả lời

6

Câu trả lời là do các ký tự ẩn nằm trong các bài học bài học.db.

Lỗi hiển thị không liên quan gì đến điều này và tôi muốn cảm ơn tất cả những người đã dành thời gian cho hai xu.

0

Tôi có hai điều cho bạn:

  1. Khi bạn nói: "Tôi biết đó là một nhiều mã nhưng tôi không thể làm việc ra tại sao điều này đang xảy ra tất cả của một đột ngột, theo nghĩa đen trong một refresh mà không có bất kỳ thay đổi nào về mã. Ngay cả khi tôi quay trở lại các phiên bản trước đó cũng làm việc điều này đang xảy ra. " Tôi muốn hỏi bạn có đang lập trình mã PHP cho bạn thông qua trình duyệt không? Nếu vậy, đôi khi bộ nhớ cache sẽ gây rối với bạn, và tôi thấy nó dễ dàng hơn nhiều để phát triển các mô hình PHP của tôi thông qua giao diện dòng lệnh PHP. Nếu đây không phải là dành cho bạn, có thể xem xét việc tắt bộ nhớ cache của trình duyệt hoàn toàn khi phát triển.
  2. Tôi nghĩ rằng sự cố bạn đang gặp phải đến từ các lỗi cú pháp trong các cột chức năng công khai của lớp Config của bạn. Tôi nghĩ bạn phải đọc về cú pháp của các trường hợp chuyển đổi http://php.net/manual/en/control-structures.switch.php ở đây bạn sẽ nhận thấy rằng bạn đã sử dụng dấu chấm phẩy sau khi bảo vệ trường hợp, nơi bạn nên sử dụng dấu hai chấm đơn giản. Cũng không cần phải phá vỡ; sau khi bạn quay lại, vì mã của bạn sẽ không bao giờ đạt đến điểm này ...

Tôi hy vọng điều này sẽ giúp - chúc may mắn ...

+0

Dấu chấm phẩy có giá trị trong các trường hợp chuyển đổi như được hiển thị trên trang bạn đã liên kết: http://php.net/manual/en/control-structures.switch.php –

+1

Ahh, okay :) Bạn học mỗi ngày. – kraenhansen

Các vấn đề liên quan