CakeFest 2024: The Official CakePHP Conference

class_parents

(PHP 5, PHP 7, PHP 8)

class_parents 与えられたクラスの親クラスを返す

説明

class_parents(object|string $object_or_class, bool $autoload = true): array|false

この関数は、与えられたクラス object_or_class の親クラス名を 配列で返します。

パラメータ

object_or_class

オブジェクトもしくはクラスの文字列を指定します。

autoload

まだロードされていない場合に オートロード するかどうか。

戻り値

成功した場合に配列、 指定されたクラスが存在しない場合に false を返します。

例1 class_parents() の例

<?php

class foo { }
class
bar extends foo {}

print_r(class_parents(new bar));

// パラメータを文字列として指定しても良い
print_r(class_parents('bar'));

spl_autoload_register();

// 'not_loaded' クラスをロードするためにオートローディングを使用する
print_r(class_parents('not_loaded', true));

?>

上の例の出力は、 たとえば以下のようになります。

Array
(
    [foo] => foo
)
Array
(
    [foo] => foo
)
Array
(
    [parent_of_not_loaded] => parent_of_not_loaded
)

注意

注意: オブジェクトが、クラスを継承しているかどうかを調べるには、 instanceof か、is_a() 関数を代わりに使うべきです。

参考

  • class_implements() - 与えられたクラスあるいはインターフェイスが実装しているインターフェイスを返す
  • is_a() - オブジェクトが指定された型のものか、部分型であるかを調べる
  • instanceof

add a note

User Contributed Notes 1 note

up
35
sergei dot solomonov at gmail dot com
11 years ago
<?php
class foo {}
class
bar extends foo {}
class
baz extends bar {}

print_r(class_parents(new baz));
?>

Will output:
Array
(
[bar] => bar
[foo] => foo
)
To Top