I did a little work to your class. but I didn't test my changes so there are probably bugs
Change Log:
1) Added initialization code that allows the user to speficy the file in the initialization and checks to make sure the requested file exists.
2) added lines and lines_calced variables so that you don't have to do the costly recalculation of the file size all the time.
3) changed print phrase to get phrase because good oop coding dictates that you should never print from within a class.
Other changes that should be made:
Each function should use the initialized variable to make sure the class was properly initialized before executing.
<?php
class random_phrase {
var $file,$lines,$lines_calced,$initialized,$error;
//let's add some code to initialize this class
//sample call $my_phrase = new random_phrase(file_name.txt);
function random_phrase($file) {
$this->file = $file;
if(file_exists($file)) {
$initialized = TRUE;
} else {
$this->initialized = FALSE;
$this->error = "Specified file does not exist";
}
$this->lines_calced = FALSE;
} //end initialization code
function get_num_lines() {
$this->lines = count(file($this->file));
$this->lines_calced = TRUE;
return $this->lines;
}
function random_num() {
if(!$this->lines_calced) {
$this->get_num_lines();
}
$random_line = rand(0, $this->lines);
return $random_line;
}
function get_phrase() {
$line = $this->random_num();
$lines = file($this->file);
return $lines[$line];
}
}
?>