runkit_class_adopt
(PECL runkit >= 0.7.0)
runkit_class_adopt — Convert a base class to an inherited class, add ancestral methods when appropriate
Description
bool runkit_class_adopt
( string
$classname
, string $parentname
)Parameters
-
classname
-
Name of class to be adopted
-
parentname
-
Parent class which child class is extending
Return Values
Returns TRUE
on success or FALSE
on failure.
Examples
Example #1 A runkit_class_adopt() example
<?php
class myParent {
function parentFunc() {
echo "Parent Function Output\n";
}
}
class myChild {
}
runkit_class_adopt('myChild','myParent');
myChild::parentFunc();
?>
The above example will output:
Parent Function Output
See Also
- runkit_class_emancipate() - Convert an inherited class to a base class, removes any method whose scope is ancestral
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Изменение поведения PHP
- runkit
- Функция Runkit_Sandbox() - Класс Runkit Sandbox -- это виртуальная машина PHP
- Функция Runkit_Sandbox_Parent() - Класс анти-песочницы для Runkit
- runkit_class_adopt
- runkit_class_emancipate
- runkit_constant_add
- runkit_constant_redefine
- runkit_constant_remove
- runkit_function_add
- runkit_function_copy
- runkit_function_redefine
- runkit_function_remove
- runkit_function_rename
- runkit_import
- runkit_lint_file
- runkit_lint
- runkit_method_add
- runkit_method_copy
- runkit_method_redefine
- runkit_method_remove
- runkit_method_rename
- runkit_return_value_used
- runkit_sandbox_output_handler
- runkit_superglobals
Коментарии
Function visibility (in PHP5) has some quirks as compared to the normal behavior with "extends". Consider the following:
<?php
class base {
public function a() { $this->b(); }
private function b() { echo "This is b()"; }
}
?>
This will work fine:
<?php
class inherit extends base {
public function c() { $this->a(); }
}
$x = new inherit;
$x->c();
?>
while this:
<?php
class adopt {
public function c() { $this->a(); }
}
runkit_class_adopt('adopt','base');
$x = new adopt;
$x->c();
?>
will generate a fatal "Call to private method base::b() from context 'adopt'" error. Protected members can be called from the inherited methods, but still cannot be called from the original class (i.e. if b() were declared protected, the example would work as written, but adopt::c() still could not call base::b() directly.
Functions such as is_subclass_of(), is_a(), and the instanceof operator also do not detect the new lineage of the object; if you are using this function to simulate multiple or dynamic inheritance, you may need to implement your own method of determining class lineage.
[EDIT by danbrown AT php DOT net: Merged addendum to post by original author.]