“函数未定义” 错误

问题描述:

我有一类是:“函数未定义” 错误

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

任何想法,为什么?

它应该是:

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

$this->checkInArr() 

该函数是一个类方法。