1

while making a module system for my CMS I stumbled upon a problem that I wish to solve in a better way (if possible).

I've got an abstract class (simplified):

 abstract class aModule {   
     function __construct() {
        include(dirname(__FILE__).'/resources/content.php');
    }
}

What I expected to happen when I instantiate child of this class, for example:

class SomeModule extends aModule {
}

$sm = new SomeModule();

Is for FILE constant to be location of SomeModule class file but instead it takes parent's, and ofcourse include doesn't work. *Note: both class files are included from external ModuleManager engine so it isn't possible for me to use $_SERVER['SCRIPT_FILENAME'].

I solved the problem the ugly way, I made abstract class constructor like this:

function __construct($fileLoc) {
    include(dirname($fileLoc).'/resources/content.php');
}

So for module to work I need to make it have constructor on its own:

function __construct() {
    parent::__construct(__FILE__);
}

This forces anyone who makes modules to include this exact constructor in his module class definition and as far as I learned, any repetition in programming is bad.

So,is there a way that my abstract class can know child classes file location or is this the only way to do it ?

j0k
  • 22,600
  • 28
  • 79
  • 90
  • 1
    http://stackoverflow.com/questions/8364246/get-filename-of-extended-class may help you – SmasherHell Nov 21 '12 at 23:38
  • Oh I searched so much but didn't include "extended" term in my searches, I forgot you can extend non-abstract classes too , thats exactly what I was looking for (if it works for abstract classes!). Sorry for this question, lock it if needed! –  Nov 22 '12 at 00:31

0 Answers0