“功能未定义”错误

时间:2012-02-04 21:57:13

标签: php php-5.3

我有一个课程:

<?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 个答案:

答案 0 :(得分:2)

$this->checkInArr() 

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

答案 1 :(得分:2)

应该是:

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

创建checkInArr();方法有点多余,但除非您打算进行更高级的检测,否则您应该在array_key_exists($needle, $arr)语句中使用if

if(array_key_exists('fox', $this->arr))
{
    echo "yes";
}
else
{
    echo "no";
}
相关问题