Page 1 of 1

Autoloading classes within module

Posted: Wed Apr 06, 2011 6:10 pm
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?

Re: Autoloading classes within module

Posted: Sat May 07, 2011 2:19 pm
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();