2011-12-15 14 views
5

पर किसी सदस्य फ़ंक्शन को कॉल करें, यहां एक बहुत ही अजीब त्रुटि प्राप्त हो रही है, मैं एक फ्लैटफाइल डेटाबेस क्लास लिख रहा हूं और यह सब ठीक काम कर रहा था जब तक कि मैं रीफ्रेश नहीं करता और अब मुझे लगातार यह संदेश मिल रहा है:PHP: घातक त्रुटि: गैर-ऑब्जेक्ट

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

मैं कक्षा के रूप में निम्नानुसार बोल रहा हूँ:

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

यहाँ मेरी class.database.php है

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

मैं जानता हूँ कि यह कोड का एक बहुत कुछ है, लेकिन मैं बाहर काम नहीं कर सकता क्यों यह सब क्या हो रहा है अचानक, शाब्दिक रूप से कोड में किसी भी बदलाव के बिना ताज़ा करें। यहां तक ​​कि अगर मैं पिछले संस्करणों के पीछे बैकट्रैक करता हूं जो भी काम करता है तो यह हो रहा है।

जब मैं करने का प्रयास:

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

यह रिटर्न:

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*

कौन सा पता चलता है कि मैं कक्षा स्तंभ है जिस पर सार्वजनिक विधि कहा जाता है के एक सदस्य पर name() प्रदर्शन कर रहा हूँ name()

जब मैं करता हूं:

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

यह लौटाता है (प्रति शब्द एक शब्द जैसा कि यह फ़ोरैच लूप में है);

first second third fourth first second third fourth

तो यह स्पष्ट रूप से इससे पहले 1 लाइन काम कर रहा है।

+5

डीबग - हम में से प्रत्येक दिन 90% तक करता है। तो आपको चाहिए – zerkms

+0

कहना मुश्किल है, आदमी। आपको त्रुटि की रेखा पर ध्यान देना चाहिए और यह पता लगाएं कि '$ यह -> _ config-> कॉलम ($ कुंजी, "स्थिति") -> name() 'किस वस्तु का काम नहीं कर रहा है। –

+0

@ मार्सियो मैंने यह देखा है लेकिन '$ यह -> _ config-> कॉलम ($ कुंजी," स्थिति ") 'कक्षा' कॉलम के सदस्य को वापस कर रहा है जिसमें एक विधि 'नाम') है। @zerkms यह अचानक त्रुटि है और कोई बदलाव नहीं होने के कारण मैं स्पष्ट रूप से इसे डीबग करने का प्रयास कर रहा हूं लेकिन जब यह काम नहीं करता है ...? –

उत्तर

6

उत्तर си.db फ़ाइल में स्थित छिपा वर्णों के कारण था।

दिखाए गए त्रुटि के साथ इसका कोई लेना-देना नहीं था और मैं उन सभी को धन्यवाद देना चाहता हूं जिन्होंने अपना दो पेंस देने के लिए समय निकाला।

0

मैं तुम्हारे लिए दो चीजें:

  1. जब आप कहते हैं: "मुझे पता है यह कोड का एक बहुत कुछ है, लेकिन मैं एक ताज़ा में बाहर काम नहीं कर सकता क्यों यह अचानक क्या हो रहा है, जिसका शाब्दिक अर्थ कोड में किसी भी बदलाव के बिना। यहां तक ​​कि अगर मैं पिछले संस्करणों के पीछे बैकट्रैक करता हूं तो यह भी हो रहा है। " मैं पूछना चाहूंगा कि क्या आप ब्राउज़र पर प्रोग्रामिंग कर रहे हैं या नहीं? यदि ऐसा है, तो कभी-कभी कैश आपके साथ गड़बड़ कर देगा, और मुझे PHP कमांड लाइन इंटरफेस के माध्यम से अपने PHP मॉडल को विकसित करना बहुत आसान लगता है। यदि यह आपके लिए नहीं है, तो हो सकता है कि विकास के दौरान अपने ब्राउज़र कैश को पूरी तरह बंद कर दें।
  2. मुझे लगता है कि आप जिस समस्या का सामना कर रहे हैं वह आपके कॉन्फ़िगर क्लास के सार्वजनिक फ़ंक्शन कॉलम में सिंटैक्स त्रुटियों से आता है। मुझे लगता है कि आपको स्विच-केस http://php.net/manual/en/control-structures.switch.php के सिंटैक्स पर पढ़ना होगा, आप देखेंगे कि आपने गार्ड गार्ड के बाद अर्धविराम का उपयोग किया है, जहां आपको केवल कॉलन का उपयोग करना चाहिए था। तोड़ने की कोई जरूरत नहीं है; आपके लौटने के बाद, क्योंकि आपका कोड कभी भी इस बिंदु तक नहीं पहुंच जाएगा ...

मुझे उम्मीद है कि यह मदद करता है - शुभकामनाएं ...

+0

सेमीकॉलन आपके द्वारा लिंक किए गए पृष्ठ पर दिखाए गए स्विच मामलों में मान्य हैं: http://php.net/manual/en/control-structures.switch.php –

+1

आह, ठीक है :) आप हर दिन सीखते हैं। – kraenhansen

संबंधित मुद्दे