php中的instance,php 中instanceof的使用
生活随笔
收集整理的這篇文章主要介紹了
php中的instance,php 中instanceof的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作用:
(1)判斷一個對象是否是某個類的實例,
(2)判斷一個對象是否實現了某個接口。
第一種用法:
$obj = new A();
if ($obj instanceof A) {
echo 'A';
}
?>
第二種用法:
interface ExampleInterface {
public function interfaceMethod();
}
class ExampleClass implements ExampleInterface {
public function interfaceMethod(){
return 'Hello World!';
}
}
$exampleInstance = new ExampleClass();
if($exampleInstance instanceof ExampleInterface){
echo 'Yes, it is';
}else{
echo 'No, it is not';
}
?>
輸出結果:Yes, it is
總結
以上是生活随笔為你收集整理的php中的instance,php 中instanceof的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 设置断点,PHP xdebug安
- 下一篇: php windows图片路径问题,解决