Dear phpbuilder user,
my intention is write Framework-System on one hand and plugin modules (classes) for the Framework on the other hand.
The target is to instantiate a PHP class by a string designation.
Enough of this poor english...lets show the code I wrote to define my questions.
The whole solution consists of four code files:
output.php
Code:
<?PHP
require ("/home/httpd/dev/cmf/cmf.class.inc");
$obj_cmf = new cls_cmf;
$obj_cmf->display_content("news", "alive");
?>
cmf.class.inc:
<?PHP
class cls_cmf {
function display_content($str_modulename, $str_dataservicename) {
include("/home/httpd/dev/cmf/" . $str_modulename . ".inc");
call_user_method($str_dataservicename, $objMODULE);
}
}
?>
news.inc:
<?PHP
if (!class_exists("cls_news")) {
include("/home/httpd/dev/cmf/news.class.inc");
}
$objMODULE = new cls_news;
?>
news.class.inc
<?PHP
class cls_news {
function alive() {
echo "I am alive !";
}
}
?>
Please copy the files in directory
and change the Path in the INCLUDE-Statements
I uses the latest php4 version.
This code works together fine. But why?Coincidence was the reason for writing the code in that way.
Including a Class definition within a Class function !?!? Where is the scope of the class ?
You can substitute the INCLUDE by REQUIRE and it also works very well.
Is this behaviour of PHP a bug or a feature ?
If calling:
$obj_cmf->display_conten("news", "alive");
in output.php again. The class definition is still in scope !?
Thank you for all requests. If more detail information is needed I will post it in the forum.
Frank Pletsch