PHP Doku:: Gets parent class - reflectionclass.getparentclass.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenReflectionThe ReflectionClass classReflectionClass::getParentClass

Ein Service von Reinhard Neidl - Webprogrammierung.

The ReflectionClass class

<<ReflectionClass::getNamespaceName

ReflectionClass::getProperties>>

ReflectionClass::getParentClass

(PHP 5)

ReflectionClass::getParentClassGets parent class

Beschreibung

public object ReflectionClass::getParentClass ( void )

Warnung

Diese Funktion ist bis jetzt nicht dokumentiert. Es steht nur die Liste der Argumente zur Verfügung.

Parameter-Liste

Diese Funktion hat keine Parameter.

Rückgabewerte

A ReflectionClass.

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
havelangep at hotmail dot com
16.12.2010 10:26
Here is a "replacement" for is_a that will additionally look both into the extended classes and in the implemented interfaces

<?php
/**
     * Check if a class extends or implements a specific class/interface
     * @param string $search The class or interface name to look for
     * @param string $className The class name of the object to compare to
     * @return bool
     */
   
function IsExtendsOrImplements( $search, $className ) {
       
$class = new ReflectionClass( $className );
        if(
false === $class ) {
            return
false;
        }
        do {
           
$name = $class->getName();
            if(
$search == $name ) {
                return
true;
            }
           
$interfaces = $class->getInterfaceNames();
            if(
is_array( $interfaces ) && in_array( $search, $interfaces )) {
                return
true;
            }
           
$class = $class->getParentClass();
        } while(
false !== $class );
        return
false;
    }
?>
jochem at drecomm dot nl
18.11.2010 13:34
When you want to find all parents (parent, parent of parent, parent of parent's parent and so on) try:

<?php
$class
= new ReflectionClass('whatever');

$parents = array();

while (
$parent = $class->getParentClass()) {
   
$parents[] = $parent->getName();
}

echo
"Parents: " . implode(", ", $parents);
?>

ReflectionClass::getParentClass() can return a ReflectionClass object of the parent class or false if no parent.

(PHP Version 5.1.6)
meecrob at k42b3 dot com
20.06.2008 12:47
When your class extends a parent class you maybe want the name
of them. Using getParentClass() is maybe a bit confusing. When
you want the name as string try the following.

<?php
$class
= new ReflectionClass('whatever');

$parent = (array) $class->getParentClass();

if(
array_key_exists('name', $parent))
{
   
# name of the parent class
   
$parent = parent['name'];
}
else
{
   
# no parent class avaible
   
$parent = false;
}
?>

When you turn getParentClass() to an array it will result either
array(0 => false) when no parent class exist or
array('name' => 'name of the parent class'). Tested on PHP 5.2.4



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)