Jump to content

Recommended Posts

I'm currently in the process of coding a module sys for my site. So far I have it detecting and loading everything perfectly. Though I've recently hit a brick wall.

 

<?php

class module
{
    var $moduleList;
    var $functionList;
    var $modCount;
    var $path = 'modules/';
    
    function module()
    {
        $this->loadModules();
    } // emd module())
    
    private function loadModules()
    {
        $handle = @opendir($this->path) or die('Unable to open '.$this->path);
        
        $this->modCount = 0;
        
        while($file = readdir($handle))
        {
         	// !!!!! Why is this working? !!!!!
         	// !is_dir($file) is detecting directories when it should not be...
         	// further investigation needed later. Perhaps a better way to check.
         	// ***slight fix by reverting back to just single file modules instead of modules in directories
            if(!is_dir($file))
            {
                $temp = explode('.', $file);
                $this->moduleList[$this->modCount] = $temp[0];
                $this->modCount++;
            }
        }
        
        closedir($handle);
    } // end loadModule())
    
    // This function is for testing purposes only. To be moved to administrator panel in the future...    
    public function listModules()
    {
        echo '<strong>'.$this->modCount.' modules loaded...</strong><br />';
        
        for($i = 0; $i < $this->modCount; $i++)
        {
            echo $this->moduleList[$i].'<br />';
        }        
    } // end listModules()
    

    public function getModCount()
    {
        return $this->modCount;
     } // end getModCount()
    
    // *** Clean this up in the future
    public function load($module)
    {
        include($this->path.$this->moduleList[$module].'.php');

        $l = new ${$this->moduleList[$module]()}; //doesn't work
        $k =  new test(); //works (temp testing)
    } // end load()
} // end class module

?>

 

My problem is in the load() function. $k works fine in creating the object, where as $l is producing:

 

Fatal error: Call to undefined function test() in C:\www\htdocs\modulesys\module.php on line 60

 

I'm trying to make it so that you only need to create the object and the rest is handled in the constructor of the class. I've tested variable variables with calling functions and such, and don't see why it shouldn't work here. Am I wrong in this thinking or did I just happen to mess up some where?

 

edit:

 

In case it helps, here are the other two files...

 

index.php

<?php

include('module.php');

$m = new module();
$m->listModules();

//implement module on/off later
for($i = 0; $i < $m->getModCount(); $i++)
{
$m->load($i);
}

?>

 

modules/test.php

<?php

class test extends module
{
    function test()
    {
	$this->display();
}
    
    function display()
    {
        echo 'Test!';
    } 
}

?>

Link to comment
https://forums.phpfreaks.com/topic/183602-variable-variables-and-objects/
Share on other sites

$l = new $this->moduleList[$module]();

 

That will work, basically using the { } wanted to look for a function, doing it like above looks for a class.

 

Ah thank you so much. I'm still kind of new to variable variables with how they work at times. Much thanks again. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.