Dynamic include()?

I hope I can explain this clearly enough. I am trying to code a class for CMS ‘framework’ to which I want to be able to have a way to “plug in” functionally. That is each instances of this class’ output function can execute code, preferably as if it was part of the class. I am using the following code:


	    function output($tmp){
	    	 $relroot=preg_replace("#processors.*#","",dirname(__FILE__));
			while ($row = $this->getRow()){
				if ($this->checkArray($row)){include ($relroot."includes/modules/RM_$tmp.php");}
			}
		}

Essentially, the idea is that the user can output or execute code as if it were a native part of the output() function by passing it the file name of the “plug-in” code.

As each object may contain rows of data (extracted from a DB), the output routine can be looped. I tried this so far and it works. I am just concerned about how I have used include() here.

  1. should this be include_once(), or will this cause conflicts if the is a loop in the object instance, or if other objects or instances include the same plug in file?

  2. what are the down side to code being included repeatedly

  3. any other pitfalls that I could or should expect?

I’d be grateful if some of the forum’s PHP experts can share their insight on this topic

include_once will not prevent all of the potential issues, but should still be used for idiot-proofing.

  1. Generation 1 will be covered by using include_once. Module designers should use include_once themselves to prevent collision on Generation 2+ includes.
  2. It should never be included repeatedly.
  3. Any ‘module’ included which fires output or executes commands directly as a result of it’s inclusion can only be called once if you use include_once (dur). Module designers must be aware to use function/method calls for any and all code.
  4. Beware of conflicting config editing. If a module relies on a charset being set, for example, and another module changes the charset, it can lead to issues. Also, obviously, uniqueness in naming functions. (These really arnt a framework-design issue, so much as a module-policing issue)

Not sure if I am dragging this OT or not, but if RM_$tmp.php is a file which is the same name as a class - then could this not be delegated to __autoload() to go away and find?

Thank you guys for the responses, but I am still quite confused.
Let me try to expound on what I was trying to say earlier, perhaps it will clear up what my actual quandary is.

As I said, my goal is to build a CMS ‘framework’ … think of it as a different version of Wordpress, for example. The premise of my framework is a master object class ( I will refer to this class as MastObj, from this point on) and a set of supporting functions. Someone developing a template using this framework would include my class definitions and my supporting functions php files. I DIDNT WANT anyone altering those two files, especially altering the file with the MastObj class , not even to expand functionality.

Consequently it occurred to me that the output of any instance of MastObj could be done via “plugged in” or included code which would be written in separate flies.

  1. Generation 1 will be covered by using include_once. Module designers should use include_once themselves to prevent collision on Generation 2+ includes.
    so module designer wouldn’t be altering the class, they would be writing only code that would plug into the loop of the output function of the MastObj class.

so for example, a template file would have this code:


...
$content=new MAstObj(mastobj parameters here...);
$sideNav=new MastObj(Mastobj parameters here...);
$footNav=new MastObj(Mastobj parameters here...);
echo'<div id="side"><ul>';
$sideNav->output('listItems');
echo'</ul></div>'
echo'<div id="main">;
$sideNav->output('content');
echo'</div>'
echo'<ul id="foot">';
$footNav->output('listItems');
echo'</ul>'

a ‘plugin’ file RM_listItems.php would be:


<? if ($this->params['headline']):?>
<li><a href="<? echo $this->params['base'].".php?cat=".$row['CatID'] ?>"><? echo $row['CatName']?></a> <? if (isset($this->params['shoCnt'])){if($this->params['shoCnt']){echo isset($this->params['eCount'][$row['CatID']])?" (".$this->params['eCount'][$row['CatID']].")":"(0)";}} ?>
</li>
<? endif?>

this would be included() into the $sideNav instance of MastObj when the function output is called. And you can see what I meant by it would execute natively, relying on properties of the class ($this->…) as if the plugh in code had been hard coded into the class.

another instance of the same class might call a completely different file for a plug in…

a ‘plugin’ file RM_content.php would be:


<? if ($this->params['headline']):?>
<? echo params['headline'];?>
<p>text</p>
<? endif?>

I was concerned that if I used include_once(), instead of include() that the "plugin"code would fail to include after the first item in the loop and OR in other instances of the class that attempt to include the same file.

At the same time… it just feels odd to call the SAME file on include to a loop. (is really is my question)

  1. Any ‘module’ included which fires output or executes commands directly as a result of it’s inclusion can only be called once if you use include_once (dur). Module designers must be aware to use function/method calls for any and all code.

I am not sure what you mean here. Again if you look at my sample plugin code… you will see it should be LIMITED to echoing, processing or returning data and independent of all other plugings ( sure it may rely on masters functions, or mastOjb methods… but that’s was my point in using include that it allows for the use of ‘$this->’ when creating a plugin.

  1. Beware of conflicting config editing. If a module relies on a charset being set, for example, and another module changes the charset, it can lead to issues. Also, obviously, uniqueness in naming functions. (These really arnt a framework-design issue, so much as a module-policing issue)

Yes that part I was aware of plug in would not be allowed to create functions. Essentially I am just trying to INJECT code into the MasterObj output function. maybe that’s what I should have said all along. lol.

So what you’re creating isnt a framework, it’s a templating system.

In that case, you absolutely -CANT- use include_once. (If you did, the second call to listItems in your code sample would return nothing.)

You are right a contemplating system is better description. :slight_smile:

you absolutely -CANT- use include_once. (If you did, the second call to listItems in your code sample would return nothing.)
I was suspecting as much.

So assuming that I police the system so that no additional FUNCTIONS or Classes are CREATED in "plugin’ code… are there things like memory drawbacks to including the same file over and over?