I was using CakePHP 2.5, and wrote a timer to time CURL request.
<?php
class AppController extends Controller {
var $started = array();
var $elapsed = array();
var $timer = array();
function timerStart( $callingMethod ) {
$this->started[ $callingMethod ] = microtime( true );
}
function timerEnd( $callingMethod ) {
if ( isset( $this->started[ $callingMethod ] ) && ! empty( $this->started[ $callingMethod ] ) ) {
$this->elapsed[ $callingMethod ] = microtime( true ) - $this->started[ $callingMethod ];
unset( $this->started[ $callingMethod ] );
$this->set( 'timer', $this->elapsed );
}
}
}
?>
<?php
class ExamplesController extends AppController {
function index() {
$this->timerStart( __METHOD__ );
// some curl request execution
$this->timerEnd( __METHOD__ );
}
}
?>
<?php
// Later somewhere in - View::Examples::Index
if ( isset( $timer ) && ! empty( $timer ) ) {
pr( $timer ); //print_r()
}
?>
Nevertheless, every time I call $this->timerStart()
I will have to provide the same parameter __METHOD__
.
It would be nice if the function declaration accepts something like function timerStart( $callingMethod = __METHOD__ )
, but the magic constant just won't work this way.
My question is, is there a way to achieve the same result? So that I can just call $this->timerStart()
without repeating the same parameter every time.