2012-02-04 148 views
1

我有一類是:「函數未定義」 錯誤

<?php 
class FileObject{ 

     private $name; 
     private $arr; 

     function __construct($name){ 

      $this->name = $name; 
     $arr = array(); 

     } 


     public function readFile(){ 
     $fileHandler = fopen($this->name, "rb"); 

     while (!feof($fileHandler)) { 

$line_of_text = fgets($fileHandler); 
$parts = explode(' ', $line_of_text); 
$count = 0; 
foreach($parts as $tokens){ 
$arr[$tokens] = $count; 
$count++; 
} 
} 

if(checkInArr("fox")) 
echo "yes"; 
else 
echo "no"; 

ksort($arr); 
print_r($arr); 
fclose($fileHandler); 
     } 

     function checkInArr($needle){ 

      if(array_key_exists($needle,$arr)) 
      return TRUE; 
      else 
      return FALSE; 

     } 

} 

?> 

,我得到這個錯誤:

Fatal error: Call to undefined function checkInArr() in C:\wamp\www\jbglobal\file_lib.php on line 29

任何想法,爲什麼?

回答

2

它應該是:

if($this->checkInArr("fox")) 
{ 
    echo "yes"; 
} 
else 
{ 
    echo "no"; 
} 

創建checkInArr();方法是有些多餘,雖然除非你打算做一些更先進的檢測,你應該在if語句中使用array_key_exists($needle, $arr)

if(array_key_exists('fox', $this->arr)) 
{ 
    echo "yes"; 
} 
else 
{ 
    echo "no"; 
} 
2
$this->checkInArr() 

該函數是一個類方法。