get parent class php

get_parent_class

(PHP 4, PHP 5, PHP 7, PHP 8)

get_parent_class — Возвращает имя родительского класса для объекта или класса

Описание

Возвращает имя родительского класса для объекта или класса.

Список параметров

Тестируемый объект или имя класса. Если вызывается из метода объекта, то этот параметр не обязателен.

Возвращаемые значения

Возвращает имя родительского класса, если object является объектом или именем класса.

Список изменений

ВерсияОписание
8.0.0Параметр object_or_class теперь принимает только объекты или корректные имена классов

Примеры

Пример #1 Пример использования get_parent_class()

class Dad <
function __construct ()
<
// реализация какой-нибудь логики
>
>

$foo = new child ();
$bar = new child2 ();

Результат выполнения данного примера:

Смотрите также

User Contributed Notes 7 notes

An output of the entire inheritance chain using closures, recursion, and OOP

class Child extends ParentClass <>
class SubChild extends Child <>
class Sub2 extends SubChild <>
class Sub3 extends Sub2 <>
class Sub4 extends Sub3 <>
class Sub5 extends Sub4 <>
class Sub6 extends Sub5 <>
class Sub7 extends Sub6 <>

Output is:
Sub7 > Sub6 > Sub5 > Sub4 > Sub3 > Sub2 > SubChild > Child > ParentClass
Sub3 > Sub2 > SubChild > Child > ParentClass

PHP (4 at least, dunno about 5) stores classnames in lower case, so:

class Bar extends Foo
<
>

echo get_parent_class ( ‘Bar’ );

echo get_parent_class ( ‘bar’ );

You can use this function to find common parent of multiple objects or classes.

//returns «F»
get_first_common_parent (array(new G (), ‘F’ ));

//returns false (non-existent class provided)
get_first_common_parent (array(new B (), ‘X’ ));
?>

I wrote a simple function doing the reverse thing: get the children:

Источник

get_parent_class

get_parent_class — Возвращает имя родительского класса для объекта или класса

Описание

Возвращает имя родительского класса для объекта или класса.

Список параметров

Тестируемый объект или имя класса

Возвращаемые значения

Возвращает имя родительского класса, если object является объектом или именем класса.

Список изменений

Примеры

Пример #1 Пример использования get_parent_class()

class dad <
function dad ()
<
// реализация какой-нибудь логики
>
>

$foo = new child ();
$bar = new child2 ();

Результат выполнения данного примера:

Смотрите также

Коментарии

If the argument obj is a string and the class is not defined, then the function returns FALSE.

If the argument obj is an object created from a class with no ancestors (or a string representing a class with no ancestors), then the function returns FALSE.

PHP (4 at least, dunno about 5) stores classnames in lower case, so:

class Bar extends Foo
<
>

echo get_parent_class ( ‘Bar’ );

echo get_parent_class ( ‘bar’ );

«‘If called without parameter outside object’ What on earth does that mean?»

There are two places this could be called:
1. From within a member function of an object. In this case, it may be called with no parameters and will return the parent class of the object owning the member function. (If the parameter is included, then it will return the parent class of the specified class as normal.)

2. From outside an object (i.e., global or function scope). In this case, PHP doesn’t know what class you’re talking about if you don’t include a parameter, so it returns FALSE. (But, of course, it works if you specify the class with the parameter.)

I wrote a simple function doing the reverse thing: get the children:

You can use this function to find common parent of multiple objects or classes.

//returns «F»
get_first_common_parent (array(new G (), ‘F’ ));

//returns false (non-existent class provided)
get_first_common_parent (array(new B (), ‘X’ ));
?>

An output of the entire inheritance chain using closures, recursion, and OOP

class Child extends ParentClass <>
class SubChild extends Child <>
class Sub2 extends SubChild <>
class Sub3 extends Sub2 <>
class Sub4 extends Sub3 <>
class Sub5 extends Sub4 <>
class Sub6 extends Sub5 <>
class Sub7 extends Sub6 <>

Output is:
Sub7 > Sub6 > Sub5 > Sub4 > Sub3 > Sub2 > SubChild > Child > ParentClass
Sub3 > Sub2 > SubChild > Child > ParentClass

Note that from PHP 5.5 you can also use `parent::class` from within a method, e.g.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *