note 112712 deleted from language.oop5.basic by nikic
| From: | nikic@php.net | Date: | Fri, 04 Jul 2014 20:56:29 +0000 |
| Subject: | note 112712 deleted from language.oop5.basic by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199856@lists.php.net to get a copy of this message | ||
Note Submitter: Achilles at TheGreatWarrior dot com
----
// Use this class to find frequency of words in a file
class WordCounter
{
const ASC = 1;
const DESC = 2;
private $words;
function __construct( $filename )
{
$file_content = file_get_contents($filename);
$this->words = array_count_values( str_word_count( strtolower($file_content) , 1 ) );
}
// Use self inside class to access static , property , method although not mandatory, you can
also use className
public function count( $order )
{
if( $order == self::ASC )
{
asort($this->words);
}
else if( $order == self::DESC )
{
arsort($this->words);
}
foreach( $this->words as $key => $value )
{
echo $key . " = " . $value ."<br>";
}
}
} // End of class WordCounter
$wc = new WordCounter("yourFileName.txt");
$wc->count( WordCounter::DESC);