我有一段代码可以计算给定 .txt 文件中的单词,但由于某些原因,我收到以下错误并且没有给出结果。我不明白出了什么问题:
Warning: arsort() expects parameter 1 to be array, null given in /Applications/MAMP/htdocs/test-php-oop/class.wordcounter.php on line 20
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/test-php-oop/class.wordcounter.php on line 21
<?php // index.php
include_once("class.wordcounter.php");
$wc = new WordCounter("words.txt");
$wc->count(WordCounter::DESC);
?>
<?php //class.wordcounter.php
class WordCounter {
const ASC = 1;
const DESC = 2;
private $words;
function __contruct($filename) {
$file_content = file_get_contents($filename);
$this->words = (array_count_values(str_word_count(strtolower($file_content),1)));
}
public function count($order) {
if ($order == self::ASC)
asort($this->words);
else if ($order == self::DESC)
arsort($this->words);
foreach ($this->words as $key => $val)
echo $key . " = " . $val . "<br/>";
}
}
?>