I am new to this site, so I apologize if this question is not welcome. I am also new to OOP
in PHP. I have done a lot of research on OOP
and I keep seeming to read different opinions on several items, especially the Scope Resolution Operator (::
) and handling class containing private functions.
When calling a private function from within the class
it seems that a majority of programmers recommend self::method()
, is this accurate? I have also seen and tested Class::method()
.
Also, I am looking for more information, or recommendations on appropriate ways to instantiate class variables, whether it be protected
, private
, var
, etc...
Take this Timer
Class
I wrote for example:
// Edit based on WouterJ's answer below.
// I believe I am all set and have a much better understanding.
class Timer {
private $_start, $_pause, $_stop, $_elapsed;
private $_laps = array();
private $_count = 1;
private $_lapTotalTime = 0;
public function __construct($start = '') {
$start = strtolower($start);
($start === 'start') ? $this->start() : NULL;
}
public function start( ) {
$this->_start = $this->getMicroTime();
}
public function stop( ) {
$this->_stop = $this->getMicroTime();
}
public function pause( ) {
$this->_pause = $this->getMicroTime();
$this->_elapsed += ($this->_pause - $this->_start);
}
public function resume( ) {
$this->_start = $this->getMicroTime();
}
// Used to build an array of times for multiple timers
public function lap($key = '') {
($key === '') ? $key = 'Lap' : $key = $key;
if (isset($this->_start)) {
$this->stop();
$this->_lapTotalTime += ($this->_stop - $this->_start);
$this->_laps[$key.' '.$this->_count] = $this->getLapTime();
$this->start();
$this->_count++;
}
}
public function getTime() {
if (!isset($this->_stop)) {
$this->_stop = $this->getMicroTime();
}
if (!empty($this->_laps)) {
$this->_laps['Total'] = $this->timeToString($this->_lapTotalTime);
return $this->_laps;
}
return $this->timeToString();
}
// PRIVATE CLASS FUNCTIONS
private function getLapTime() {
return $this->timeToString();
}
private function getMicroTime( ) {
list($usec, $sec) = explode(' ', microtime());
return ((float) $usec + (float) $sec);
}
private function timeToString($seconds = '') {
if ($seconds === '') {
$seconds = ($this->_stop - $this->_start) + $this->_elapsed;
}
$seconds = $this->roundMicroTime($seconds);
$hours = floor($seconds / (60 * 60));
$divisor_for_minutes = $seconds % (60 * 60);
$minutes = floor($divisor_for_minutes / 60);
return $hours . "h:" . $minutes . "m:" . $seconds . "s";
}
private function roundMicroTime($microTime) {
return round($microTime, 4, PHP_ROUND_HALF_UP);
}
}
All of my instantiated variables are declared as protected
. My reasoning, if I am understanding correctly is because although this Class doesn't extend
another Class, the other Class may extend
Timer. Am I on the right track?
Example of Timer use with laps:
$t = new Timer('start');
usleep(3215789);
// Declare a key for the lap array
$t->lap('Sleep');
usleep(4445666);
// No key declaration
$t->lap();
usleep(1000000);
echo "<pre>";
if (is_array($t->getTime())) {
print_r($t->getTime());
} else {
echo $t->getTime();
}
echo "</pre>";
Output:
Array
(
[Sleep 1] => 0h:0m:3.2154s
[Lap 2] => 0h:0m:4.4462s
[Total] => 0h:0m:7.6616s
)
Without laps:
$t = new Timer('start');
usleep(3215789);
usleep(1000000);
echo "<pre>";
if (is_array($t->getTime())) {
print_r($t->getTime());
} else {
echo $t->getTime();
}
echo "</pre>";
Output:
0h:0m:4.2158s
::
operator, orT_PAAMAYIM_NEKUDOTAYIM
if you like that, is an operator for static methods and class constants, to call a method use$this->method()
. – Wouter J Mar 13 at 15:09