Autoloading classes within module

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
cve
Forum Members
Forum Members
Posts: 44
Joined: Wed Jul 07, 2010 10:54 am

Autoloading classes within module

Post by cve »

Hi guys, how can I make autoloading classes in my new module? I was typing
in my main module folder a have Somethink.php file with this content:

Code: Select all

<?php 
class Somethinkg {
    public function __construct() {
        echo "Somethink";
    }
}
And in my main module class in action.defaultadmin.php somewhere I have:

Code: Select all

<?php
function __autoload($classname) {
    require_once $classname . '.php';
}

$var  = new Something();
And, when I fire up my code is doing nothink, no errors, no exceptions... nothink... What I doing wrong?
irish
Forum Members
Forum Members
Posts: 101
Joined: Tue Jun 03, 2008 2:31 pm

Re: Autoloading classes within module

Post by irish »

This may not be the best way, but here is how I do it.

In the MODULENAME.module.php before the class opener put in the include file to the class you want to autoload.

Code: Select all

include_once(dirname(__FILE__).'/Another.class.php');
class MyModule extends CMSModule
On the same file add the construct and initiate the new class.

Code: Select all

    function __construct(){
		parent::CMSModule();
   		$this->AnotherClass = new Another();		
    }
Now on any of you pages you can call $this->AnotherClass->method();
Post Reply

Return to “Developers Discussion”