简体   繁体   中英

“Function not defined” error

I have a class which is :

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

        }

}

?>

and I'm getting this error:

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

Any ideas why?

$this->checkInArr() 

由于此函数是一个类方法。

It should be:

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

Creating the checkInArr(); method is somewhat redundant though unless you plan to do some more advanced detection, you should just use array_key_exists($needle, $arr) in that if statement.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM