我想测量PHP中某个编码任务的时间。我写了下面的代码来测量时间。
<?php
class encode_logs_vo {
public $id_host, $id_vhost, $id_contents, $serverid;
public function __construct($id_host, $id_vhost, $id_contents, $serverid) {
$this->id_host = $id_host;
$this->id_vhost = $id_vhost;
$this->id_contents = $id_contents;
$this->serverid = $serverid;
}
}
class encode_logs {
private $start_microtime = 0;
public function __construct(encode_logs_vo $encode_logs_vo) {
$this->encode_logs_vo = $encode_logs_vo;
}
public function start_measuring() {
MDB::execute(encode_logs_sql::insert($this->encode_logs_vo->id_host, $this->encode_logs_vo->id_vhost, $this->encode_logs_vo->id_contents, $this->encode_logs_vo->serverid));
$this->start_microtime = microtime(true);
}
public function stop_measuring() {
MDB::execute(encode_logs_sql::update($this->encode_logs_vo->id_host, $this->encode_logs_vo->id_vhost, $this->encode_logs_vo->id_contents, $this->encode_logs_vo->serverid, microtime(true) - $this->start_microtime));
$this->start_microtime = 0;
}
public function update_measuring($exec_time) {
MDB::execute(encode_logs_sql::update($this->encode_logs_vo->id_host, $this->encode_logs_vo->id_vhost, $this->encode_logs_vo->id_contents, $this->encode_logs_vo->serverid, $exec_time));
}
public static function clear($interval_month) {
MDB::execute(encode_logs_sql::delete($interval_month));
}
}
class encode_logs_sql {
static function insert($id_host, $id_vhost, $id_contents, $serverid) {
$now = date('Y-m-d H:i:s');
$sql = <<<SQL
INSERT INTO
encode_logs(
id_host, id_vhost, id_contents, serverid, create_date
) VALUES (
{$id_host}, {$id_vhost}, {$id_contents}, '{$serverid}', '{$now}'
) ON DUPLICATE KEY UPDATE
update_date = '{$now}'
SQL;
return $sql;
}
static function update($id_host, $id_vhost, $id_contents, $serverid, $exec_time) {
$now = date('Y-m-d H:i:s');
$sql = <<<SQL
INSERT INTO
encode_logs(
id_host, id_vhost, id_contents, serverid, exec_time, create_date
) VALUES (
{$id_host}, {$id_vhost}, {$id_contents}, '{$serverid}', '{$exec_time}', '{$now}'
) ON DUPLICATE KEY UPDATE
exec_time = exec_time + '{$exec_time}'
SQL;
return $sql;
}
static function delete($interval_month) {
$interval_month = -1 * $interval_month;
$sql = <<<SQL
DELETE
FROM
encode_logs
WHERE
update_date < DATE_ADD(NOW(),INTERVAL {$interval_month} MONTH)
SQL;
return $sql;
}
}
我以下列方式使用代码
$encode_logs = new encode_logs(new encode_logs_vo($id_host, $id_vhost, $id_contents, $serverid="some server"));
$encode_logs->start_measuring();
//do Some task
$encode_logs->stop_measuring();
如果所有这些都位于同一个文件中,则上述工作非常有效。 现在我想从一个功能开始测量时间并停止另一个功能的测量。所以,我试过这个:
function one(){
$encode_logs = new encode_logs(new encode_logs_vo($id_host, $id_vhost, $id_contents, $serverid="some server"));
$encode_logs->start_measuring();
$data = array(
"param1" => "something",
"encode_logs" => $encode_logs
);
two(json_encode($data));
}
function two($data){
//do something
$data = json_decode($data);
$param1 = $data->param1;
$encode_logs = $data->encode_logs
$encode_logs->stop_measuring(); //error at this point
}
我尝试在function two
进行类型转换,但在停止测量时仍会出现以下错误。
Call to undefined method stdClass::stop_measuring()
答案 0 :(得分:1)
正如聊天中所解释的那样,实际发生的事情是one()
和two()
之间的往返是json_encode
/ json_decode
和预期内容的消息队列作为一个字符串。
这意味着one()
中的原始对象实例不再存在于two()
中,这有很多含义:
另一种方法是使用serialize和unserialize。通过使用php-native序列化方法,可以持久保存类信息。必须小心谨慎,因为这也可能是有损的,但可以保留更多的信息;特别是,关联数组将保持原样,而不是像JSON函数一样转换为对象。