Question on how to make a module with an API

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
kendo451

Question on how to make a module with an API

Post by kendo451 »

If I am building a module and put a folder called lib with some classes in it, will they be automatically included?

Any suggestions on how to make the methods available to other modules?
NaN

Re: Question on how to make a module with an API

Post by NaN »

kendo451 wrote: If I am building a module and put a folder called lib with some classes in it, will they be automatically included?
No.
You need to include them by yourself.
kendo451 wrote: Any suggestions on how to make the methods available to other modules?
What about creating a function that includes the class file? And one function to access certain functions of that class? Here an example...
Lets say you have two modules. ModuleA and ModuleB.
ModuleA has some classes that should be used by ModuleB.
So in ModuleA you can create a function to load the class and one function to access the classes methods:

Code: Select all


var $ClassA;
var $ClassB;
var $ClassC;

function LoadClass($className, $someVars) {
   if(isset($this->$className) && !$this->$className) {
      require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $className'.php');
      $this->$className = new $$className($someVars);
   }
   return $this->$className;
}

function DoSomething($someVars) {
   $classA =& $this->LoadClass('ClassA',$someVars);
   return $classA->DoSomething($someVars);
}

ModuleB can use the classes this way:

Code: Select all


$moduleA =& $this->GetModuleInstance('ModuleA');
$foo = $moduleA->DoSomething($bar);

So ModuleB can use external classes of ModuleA.
Post Reply

Return to “Developers Discussion”