为什么使用instanceof调用需要这么多时间?

时间:2016-10-21 14:30:29

标签: php

我不知道为什么instanceof来电需要这么多时间。例如:

if($this instanceof Player){
    // Lags the server.
}

但是如果我在播放器类中添加它:

public function isPlayer(){
    return true;
}

然后做:

if($this->isPlayer()){
    //Super fast.
}

速度快得多。那么,当我if ($this instanceof Player)在那段时间内服务器真正做了什么?

1 个答案:

答案 0 :(得分:0)

PHP使用字符串比较进行instanceof检查。

如果您有很长的类名或经常调用它可能会有明显的减速。请查看https://phpgoodness.wordpress.com/2010/07/23/some-php-performance-myths/的Instanceof部分,有一个性能比较,显示差异。