";函数未定义“;错误


"Function not defined" error

我有一个类,它是:

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

因为这个函数是一个类方法。