CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    [RESOLVED] How can this be done?

    I'm coding my CMS in PHP4, I will release it under GPL license as son as I think the core API is stable. Right now I change it allot in the search for a better architecture.

    I need an OOP way to be able to load modules at runtime. For example:

    PHP Code:
    class CMS {
         function 
    load_module($module_file) {
              
    // loaded module can now be used by CMS class or 
              // by any previously loaded module
         
    }

    this way i can do:

    PHP Code:
    $cms = new CMS;
    $cms->load_module("config.php"); //  load first module, config
    $cms->load_module("database.php"); // load second module, database, it has acces to cms->config module some how
    $cms->load_module("templating.php"); // load third module, templating, which can use config and database modules
    // etc... 
    I've been reading about Drupal architecture here (make sure to read the comments also), but Drupal is what I call, function oriented lol. It's strictly procedural (still works very well).

    What I really needed is a true OOP way to do it, something like the code example above. I know about aggregate() for PHP4 and runkit for PHP5 but they are experimental and runkit doesn't even bundles with PHP. So those are a no-go for a OOS CMS (Open Source Content Management System).

    Any ideas?

    Thanks in advance for your attention.
    Last edited by bubu; May 30th, 2007 at 05:19 PM.
    All consequences are eternal in some way.

  2. #2
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    Re: How can this be done?

    I've been trying to implement a crazy theory of mine to solve my problem, since yesterday and only now I realize this wount work and generates an infinite recusion. It's fun to see the output:


    PHP Code:
    class cms
    {
      var 
    $html 'none';

      function 
    load_module($classname$objname) {
        
    $this->$objname = new $classname(&$this);
      }
    }


    class 
    module
    {
        var 
    $cms;

        function 
    module(&$cms_instance) {
          
    $this->cms = &$cms_instance;
        }
        
        function 
    write_something() {
          
    $this->cms->html 'module 1';
        }
    }

    class 
    module2
    {
        var 
    $cms;

        function 
    module2(&$cms_instance) {
          
    $this->cms = &$cms_instance;
        }

        function 
    write_something() {
          
    $this->cms->html 'module2';
        }
    }

    $cms = new cms;
    echo 
    "\n\n".'$cms = new cms;'."\n";
    var_dump($cms);

    $cms->load_module('module''mod1');
    echo 
    "\n\n".'$cms->load_module(\'module\', \'mod1\');'."\n";
    var_dump($cms);

    $cms->mod1->write_something();
    echo 
    "\n\n".'$cms->mod1->write_something();\n'."\n";
    var_dump($cms);

    $cms->load_module('module2''mod2');
    echo 
    "\n\n".'$cms->load_module(\'module2\', \'mod2\');'."\n";
    var_dump($cms);

    $cms->mod1->write_something();
    echo 
    "\n\n".'$cms->mod1->write_something();'."\n";
    var_dump($cms); 
    Outputs:

    Code:
    $cms = new cms;
    object(cms)(1) {
      ["html"]=>
      string(4) "none"
    }
    
    
    $cms->load_module('module', 'mod1');
    object(cms)(2) {
      ["html"]=>
      string(4) "none"
      ["mod1"]=>
      object(module)(1) {
        ["cms"]=>
        &object(cms)(2) {
          ["html"]=>
          string(4) "none"
          ["mod1"]=>
          object(module)(1) {
            ["cms"]=>
            &object(cms)(2) {
              ["html"]=>
              string(4) "none"
              ["mod1"]=>
              *RECURSION*
            }
          }
        }
      }
    }
    
    
    $cms->mod1->write_something();\n
    object(cms)(2) {
      ["html"]=>
      string(8) "module 1"
      ["mod1"]=>
      &object(module)(1) {
        ["cms"]=>
        &object(cms)(2) {
          ["html"]=>
          string(8) "module 1"
          ["mod1"]=>
          &object(module)(1) {
            ["cms"]=>
            &object(cms)(2) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              *RECURSION*
            }
          }
        }
      }
    }
    
    
    $cms->load_module('module2', 'mod2');
    object(cms)(3) {
      ["html"]=>
      string(8) "module 1"
      ["mod1"]=>
      &object(module)(1) {
        ["cms"]=>
        &object(cms)(3) {
          ["html"]=>
          string(8) "module 1"
          ["mod1"]=>
          &object(module)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              *RECURSION*
              ["mod2"]=>
              object(module2)(1) {
                ["cms"]=>
                *RECURSION*
              }
            }
          }
          ["mod2"]=>
          object(module2)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              &object(module)(1) {
                ["cms"]=>
                *RECURSION*
              }
              ["mod2"]=>
              object(module2)(1) {
                ["cms"]=>
                *RECURSION*
              }
            }
          }
        }
      }
      ["mod2"]=>
      object(module2)(1) {
        ["cms"]=>
        &object(cms)(3) {
          ["html"]=>
          string(8) "module 1"
          ["mod1"]=>
          &object(module)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              &object(module)(1) {
                ["cms"]=>
                *RECURSION*
              }
              ["mod2"]=>
              object(module2)(1) {
                ["cms"]=>
                *RECURSION*
              }
            }
          }
          ["mod2"]=>
          object(module2)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              &object(module)(1) {
                ["cms"]=>
                *RECURSION*
              }
              ["mod2"]=>
              *RECURSION*
            }
          }
        }
      }
    }
    
    
    $cms->mod1->write_something();
    object(cms)(3) {
      ["html"]=>
      string(8) "module 1"
      ["mod1"]=>
      &object(module)(1) {
        ["cms"]=>
        &object(cms)(3) {
          ["html"]=>
          string(8) "module 1"
          ["mod1"]=>
          &object(module)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              *RECURSION*
              ["mod2"]=>
              object(module2)(1) {
                ["cms"]=>
                *RECURSION*
              }
            }
          }
          ["mod2"]=>
          object(module2)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              &object(module)(1) {
                ["cms"]=>
                *RECURSION*
              }
              ["mod2"]=>
              object(module2)(1) {
                ["cms"]=>
                *RECURSION*
              }
            }
          }
        }
      }
      ["mod2"]=>
      object(module2)(1) {
        ["cms"]=>
        &object(cms)(3) {
          ["html"]=>
          string(8) "module 1"
          ["mod1"]=>
          &object(module)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              &object(module)(1) {
                ["cms"]=>
                *RECURSION*
              }
              ["mod2"]=>
              object(module2)(1) {
                ["cms"]=>
                *RECURSION*
              }
            }
          }
          ["mod2"]=>
          object(module2)(1) {
            ["cms"]=>
            &object(cms)(3) {
              ["html"]=>
              string(8) "module 1"
              ["mod1"]=>
              &object(module)(1) {
                ["cms"]=>
                *RECURSION*
              }
              ["mod2"]=>
              *RECURSION*
            }
          }
        }
      }
    }
    Note all the *RECURSION*. LOL. Back to 0% progress. Right now I'm gonna try another crazy theory...
    Last edited by bubu; May 30th, 2007 at 06:59 PM.
    All consequences are eternal in some way.

  3. #3
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    Re: How can this be done?

    My other theory, which is based on my last one posted above, also failed by generating infinite recursion again. The main problem i'm facing is to enable access from one module to other. Like to enable access from $objmain->obj1 to $objmain->obj2. Normally obj1 doesn't know that obj2 exists and telling each one of them about the other modules in the class generates infinite recursion. Because it goes: i know you that knows me that knows you that knows me that knows you... endless.

    I know i'm facing this problem from one point of view and I already know that point of view is wrong. I need to start looking at this problem from different points.
    All consequences are eternal in some way.

  4. #4
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    Thumbs up Re: How can this be done?

    I solved my problem! I created a nice module loading class that let modules interact with they self using a singleton pattern. It works like a charm!

    If you need it you can see the solution in my blog. I just posted it there.

    Thanks for all those who read my post in CG
    All consequences are eternal in some way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured