What is the difference between is_a and instanceof?

后端 未结 9 920
难免孤独
难免孤独 2020-12-04 07:23

I am aware that instanceof is an operator and that is_a is a method.

Is the method slower in performance? What would you prefer to use?

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 07:37

    I can't speak for performance -- I haven't measured anything yet -- but depending on what you are attempting, there are limitations with instanceof. Check out my question, just recently, about it:

    PHP 'instanceof' failing with class constant

    I've ended up using is_a instead. I like the structure of instanceof better (I think it reads nicer) and will continue to use it where I can.

提交回复
热议问题