©
本文档使用 PHP中文网手册 发布
(PECL runkit >= 0.7.0)
runkit_import — Process a PHP file importing function and class definitions, overwriting where appropriate
$filename
[, int $flags
= RUNKIT_IMPORT_CLASS_METHODS
] )
Similar to include however any code residing outside
of a function or class is simply ignored.
Additionally, depending on the value of flags
,
any functions or classes which already exist in the currently running environment
may be automatically overwritten by their new definitions.
filename
Filename to import function and class definitions from
flags
Bitwise OR of the RUNKIT_IMPORT_* family of constants.
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 runkit_import() example
<?php
// import classes entirely
runkit_import ( 'myfile.inc' , RUNKIT_IMPORT_CLASSES );
runkit_import ( 'myfile.inc' , RUNKIT_IMPORT_CLASSES & ~ RUNKIT_IMPORT_CLASS_STATIC_PROPS );
runkit_import ( 'myfile.inc' , RUNKIT_IMPORT_CLASS_STATIC_PROPS );
?>
[#1] dabbers [2008-08-15 16:58:33]
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.
[#2] Maxdamantus [2007-11-25 22:35:51]
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");
?>
[#3] info at lucasvd dot nl [2007-02-13 07:13:08]
Note that reloading classes does not work, when you're using this extension on the PHP Command Line Interface.
[#4] php at ransico dot com [2006-11-02 05:17:47]
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.
[#5] bbisgod [at] gmail [dot] com [2005-11-29 11:19:59]
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.
[#6] bbisgod [at] gmail [dot] com [2005-11-29 10:27:31]
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;
}
}
}
?>