"If called without parameter outside object" What on earth does that mean?
What I can tell you, and that is not documented, is that if the object in question does not have an explicitly declared parent class, it does return boolean false. It doesn't for example return 'stdClass' on the basis that all objects are derived from that.
get_parent_class
(PHP 4, PHP 5)
get_parent_class — オブジェクトの親クラスの名前を取得する
パラメータ
- object
-
調べたいオブジェクトあるいはクラスの名前。
返り値
object がインスタンスあるいは名前であるクラスの親クラス名を返します。
注意: オブジェクトが親を持たない場合は FALSE を返します。
オブジェクトの外部からこのパラメータを省略してコールすると、 この関数は FALSE を返します
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.0 より前 | オブジェクトの外部からパラメータなしでコールすると、 この関数は警告を発生したうえで NULL を返します。 |
| 5.0.0 以降 | オブジェクトのメソッドからコールされた場合、パラメータ object はオプションとなります。 |
| 4.0.5 以降 | object が文字列の場合、 その名前のクラスの親クラスの名前を返します。 |
例
例1 get_parent_class() の使用例
<?php
class dad {
function dad()
{
// ロジックを実装する
}
}
class child extends dad {
function child()
{
echo "I'm " , get_parent_class($this) , "'s son\n";
}
}
class child2 extends dad {
function child2()
{
echo "I'm " , get_parent_class('child2') , "'s son too\n";
}
}
$foo = new child();
$bar = new child2();
?>
上の例の出力は以下となります。
I'm dad's son I'm dad's son too
get_parent_class
marcus at synchromedia dot co dot uk
17-Apr-2008 01:08
17-Apr-2008 01:08
birkholz at web dot de
07-Oct-2005 09:01
07-Oct-2005 09:01
tim at correctclick dot com wrote:
<quote>
A slightly more cryptic but faster get_ancestors function:
<?php
function get_ancestors ($class) {
for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
return $classes;
}
?>
(The second part of the for is implicitly testing for $class != ""). Recursion is considerably slower than looping, so you probably want to use this function.
Hope someone finds it useful.
</quote>
I would prefer this version, because it will create no duplicates:
<?php
function get_ancestors ($class) {
$classes = array($class);
while($class = get_parent_class($class)) { $classes[] = $class; }
return $classes;
}
Greets, Dennis
?>
matt-php at DONT-SPAM-ME dot bitdifferent dot com
02-Nov-2004 12:52
02-Nov-2004 12:52
PHP (4 at least, dunno about 5) stores classnames in lower case, so:
<?PHP
class Foo
{
}
class Bar extends Foo
{
}
echo get_parent_class('Bar');
echo "\n";
echo get_parent_class('bar');
?>
will output:
foo
foo
radu dot rendec at ines dot ro
07-Apr-2004 10:44
07-Apr-2004 10:44
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.
tim at correctclick dot com
06-Apr-2003 12:48
06-Apr-2003 12:48
A slightly more cryptic but faster get_ancestors function:
function get_ancestors ($class) {
for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
return $classes;
}
(The second part of the for is implicitly testing for $class != ""). Recursion is considerably slower than looping, so you probably want to use this function.
Hope someone finds it useful.
eric dot brison at anakeen dot com
28-Jan-2002 09:14
28-Jan-2002 09:14
To return all ancestors class of an object
function get_ancestors_class($classname) {
$father = get_parent_class($classname);
if ($father != "") {
$ancestors = get_ancestors_class($father);
$ancestors[] = $father;
}
return $ancestors;
}
example :
-----------
Class C {
}
Class B extends C {
}
Class A extends B {
}
print_r (get_ancestors_class("a"));
print_r (get_ancestors_class("b"));
example result :
---------------
Array
(
[0] => c
[1] => b
)
Array
(
[0] => c
)