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.]
runkit_class_adopt
(PECL runkit >= 0.7.0)
runkit_class_adopt — Convertir una clase base en una clase heredada, añadiendo métodos ancestrales cuando sea apropiado
Descripción
bool runkit_class_adopt
( string
$classname
, string $parentname
)Parámetros
-
classname -
Nombre de la clase a ser adoptada
-
parentname -
La clase padre en la que se extiende la hija
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Ejemplos
Ejemplo #1 Un ejemplo de runkit_class_adopt()
<?php
class miPadre {
function funcPadre() {
echo "Salida de la Función Padre\n";
}
}
class miHija {
}
runkit_class_adopt('myChild','funcPadre');
miHija::funcPadre();
?>
El resultado del ejemplo sería:
Salida de la Función Padre
Ver también
- runkit_class_emancipate() - Convertir una clase heredada en una clase base, eliminando cualquier método cuyo ámbito sea ancestral
muramas at cyberdiscordia dot org ¶
7 years ago
