is_subclass_of()function 是一個內置函數PHP檢查對象是否繼承或實現指定的類。
用法:
bool is_subclass_of(
mixed $object_or_class,
string $class,
bool $allow_string = true
)
參數:該函數接受三個參數,如下所述:
- $對象或類:可以指定類名或對象實例,如果指定的類不存在,則不會產生錯誤。
- $class: 該參數以字符串格式指定類名。
- $allow_string: 如果該參數設置為,則不能使用字符串類名或對象錯誤的。如果該類不存在,則此參數將阻止調用自動加載器。
返回值:如果對象或類屬於父類,該函數將返回“true”;否則,它將返回“false”。
示例 1:此示例演示了 PHP is_subclass_of() 函數。
PHP
<?php
class GeeksforGeek {
var $color = "Red";
}
class Articles extends GeeksforGeek {
var $user = 'Test';
}
$ob = new GeeksforGeek();
$article = new Articles();
if (is_subclass_of($article, 'GeeksforGeek')) {
echo "Yes, Article is subclass of GeeksforGeek";
}
else {
echo "No, Articles is not subclass of GeeksforGeek";
}
?>
輸出:
Yes, Article is subclass of GeeksforGeek
示例 2:以下代碼演示了 PHP is_subclass_of() 方法的另一個示例。
PHP
<?php
interface GeeksforGeek {
public function hello();
}
class Articles implements GeeksforGeek {
function hello() {
echo "Hey GeeksforGeeks";
}
}
$ob = new Articles;
if (is_subclass_of($ob, 'GeeksforGeek')) {
echo "Yes, Articles implements GeeksforGeek interface\n";
}
else {
echo "No, Articles do not implement GeeksforGeek interface\n";
}
if (is_subclass_of($ob, 'Network')) {
echo "Yes, Articles implements Network interface";
}
else {
echo "No, Articles do not implement Network interface";
}
?>
輸出:
Yes, Articles is implement GeeksforGeek interface No, Articles do not implement Network interface
參考: https://www.php.net/manual/en/function.is-subclass-of.php
相關用法
- PHP is_subclass_of()用法及代碼示例
- PHP is_scalar()用法及代碼示例
- PHP is_string()用法及代碼示例
- PHP is_finite()用法及代碼示例
- PHP is_infinite()用法及代碼示例
- PHP is_nan()用法及代碼示例
- PHP is_file()用法及代碼示例
- PHP is_uploaded_file()用法及代碼示例
- PHP is_integer()用法及代碼示例
- PHP is_long()用法及代碼示例
- PHP is_resource()用法及代碼示例
- PHP is_writeable()用法及代碼示例
- PHP is_readable()用法及代碼示例
- PHP is_link()用法及代碼示例
- PHP is_dir()用法及代碼示例
- PHP is_a()用法及代碼示例
- PHP is_callable()用法及代碼示例
- PHP is_float()用法及代碼示例
- PHP is_null()用法及代碼示例
- PHP is_numeric()用法及代碼示例
- PHP is_writable()用法及代碼示例
- PHP is_array()用法及代碼示例
- PHP is_bool()用法及代碼示例
- PHP is_executable()用法及代碼示例
- PHP is_finite(), is_infinite(), is_nan()用法及代碼示例
注:本文由純淨天空篩選整理自neeraj3304大神的英文原創作品 PHP is_subclass_of() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。