runkit_import

(No version information available, might only be in Git)

runkit_import Procesar un archivo PHP importando definiciones de funciones y clases, sobrescribiendo cuando sea apropiado

Descripción

runkit_import(string $filename, int $flags = RUNKIT_IMPORT_CLASS_METHODS): bool

Similar a include, sin embargo cualquier código que resida fuera de una función o clase es simplemente ignorado. Además, dependiendo del valor de flags, cualquier función o clase ya existente en el entorno de ejecución actual podría ser sobrescrito automáticamente por sus nuevas definiciones.

Parámetros

filename

El nombre de archivo desde que se va a importar las definiciones de funciones y clases

flags

Operador OR a nivel de bit de la familia de constantes RUNKIT_IMPORT_*.

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de runkit_import()

<?php
// importar clases completamente
runkit_import('myfile.inc', RUNKIT_IMPORT_CLASSES);

/* importar clases, aunque no importar sus propiedades estáticas
(RUNKIT_IMPORT_CLASS_STATIC_PROPS está disponible desde 1.0.1) */
runkit_import('myfile.inc', RUNKIT_IMPORT_CLASSES & ~RUNKIT_IMPORT_CLASS_STATIC_PROPS);

/* importar solamente las propiedades estáticas de las clases
(RUNKIT_IMPORT_CLASS_STATIC_PROPS está disponible desde 1.0.1) */
runkit_import('myfile.inc', RUNKIT_IMPORT_CLASS_STATIC_PROPS);
?>

add a note add a note

User Contributed Notes 6 notes

up
2
bbisgod [at] gmail [dot] com
18 years ago
I was experiencing problems using this function on a script. I discovered through trial and error that you CANNOT reload a function (or method of a class) if it has been called (e.g, its present in the debug_backtrace). Also you cannot redeclare a function that is used by set_error_handler.

The reasons are logical, but it took me a good 2 days of debugging to find it, hope this saves someone a headache.
up
1
dabbers
15 years ago
It appears that this function still doesn't remove the previous class. I'm not sure what it does to it, but looking at memory usage, it only goes up instead of staying the same or going down (This was when I reloaded a class that was exactly the same).

So that might be something to fix, unless I'm doing something wrong.
up
0
Maxdamantus
16 years ago
In reply to the comment made by info at lucasvd dot nl:

Runkit WILL reload classes, but the runkit_import must be called from inside an other class or object to do so.

<?php
class reload{
    function
__construct($file){
       
runkit_import($file);
    }
}

new
reload("myclassfile.php");
?>
up
0
info at lucasvd dot nl
17 years ago
Note that reloading classes does not work, when you're using this extension on the PHP Command Line Interface.
up
0
php at ransico dot com
17 years ago
When using this function to override an existing class, you need to be careful in cases where the new definition 'extends' another class - it won't work.

For example,

<?php
// File 1

class BaseCls { }

class
TestCls extends BaseCls {
  function
hi () { echo "Hi"; }
}

runkit_import('test2.php');

?>

<?php
// File 2
class TestCls extends BaseCls {
  function
hi () { echo "Hi again!"; }
}
?>

will NOT work. In file two, you need to omit the 'extends BaseCls'. Note however, that anything from BaseCls will still be in TestCls since it was defined originally in file 1.

From what I can tell, runkit_import defines and overwrites elements - however it does not delete.
up
0
bbisgod [at] gmail [dot] com
18 years ago
Heres a nice function to reload the whole program, note, requires PHP5.1:

<?php
function reload() {
 
$files = get_included_files();
  foreach(
$files as $file) {
    if (
runkit_lint_file($file)) {
     
runkit_import($file);
    } else {
      return
false;
    }
  }
}
?>
To Top