Jump to content

need extended class working by calling itself instead of extender class(es)


ozone

Recommended Posts

Hey again. I faced a problem again. Here we go.. :

 

I have main class "core" in file site.php

 

above in this file, is includes, of other future modules (articles, forums etc...)

 

There are classes in included files. So I need somehow to extend main class "core" with other 5 or 10 classes in included files, so I can work properly...

 

It is kind a CMS mini version for me. I tried  :rtfm: - no good :-\

 

//including all classes in include folder
foreach(glob("*.class.php") as $class_filename) {
   require_once($class_filename);
}

//main web site control and output class
class website {
   function __construct(){
      //get variables from link
      $pre_link = explode('//', $_SERVER['REDIRECT_URL']);
      $link = explode('/', $pre_link[1]);

      //remove .html from the end
      foreach ($link as $sub_link => $address){
         $pat = "/html/i";
         if(preg_match($pat, $address)){
            $link[$sub_link] =  str_replace('.html', '', $address);
         }
         //$link[0] = website.com
         //$link[1] = first_variable between "/"..
         
         //last variable most often index or link for SEO
         $link['last'] = $link[count($link)];
      }
      //deside module of throw error
      
   }

 

This is part of core.php file, where is constructor, at this time class contains no more functions...

 

I need advice how to extend main class without calling extenders, becouse there will be 10 classes who extends main "core" class. Thanks in advance...

 

EDITED BY akitchin: just fixing code tags.

Link to comment
Share on other sites

Here is a better version: http://pastebin.com/f12cbc64c. It assumes this directory structure:

modules
- yourModuleName
   - controllerName
      - actionName.php
      - index.php // every controllerName directory must contain a index.php or it will direct you to /default/error/error(.php)
- default
   - error
      error.php
   - index
      index.php
   
index.php //code from http://pastebin.com/f12cbc64c

 

A starter's edition can be downloaded here: http://rapidshare.com/files/256867366/babysteps.zip (Don't mind the name it's part of a project of mine)

Link to comment
Share on other sites

I want to make fully automatic module system;

Just upload php class file to core directory and core class would find and extend main "core" class with class found in module file...

 

class Core implements ArrayAccess, Countable, Iterator/*SPL Flavored*/ {
    
    protected $_modules = array();
    
    public function loadModules($modulePattern = '*.mod.php') {
        foreach (glob($modulePattern) as $moduleFile) {
            require_once($moduleFile);
            $filename = pathinfo($moduleFile, PATHINFO_FILENAME);
            $className = ucfirst($filename);
            if (class_exists($className)) {
                $object = new $className($this);//TODO: use reflection to identify the parent
                if ($object instanceof Core) {
                    $this->addModule($object);
                }
            }
        }
    }
    
    public function addModule(Core $module) {
        $moduleClassName = get_class($module);
        $this->_modules[$moduleClassName] = $module;
    }
    
    public function moduleIsLoaded($moduleName) {
        return array_key_exists($moduleName, $this->_modules);
    }
    
    public function getModule($moduleName) {
        return $this->_modules[$moduleName];
    }
    
    //TODO: implement ArrayAccess, Countable and Iterator methods
}

Link to comment
Share on other sites

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.