Page 1 of 1

Decreasing footprint of memory by using classes. But how?

Posted: Mon Aug 15, 2011 8:54 am
by Duketown
Hi,

I'm working on the upgrade of one of my modules to work against CMSMS vs 1.10.
Up until now I have been using the SVN version of the core to get it up and running.
My Shop Made Simple module has the functionality to load images of products.
In the latest CMSMS versions trying to decrease needed internal memory has been introduced. I have been looking, as an example, to module News.
It has a lib/class.news_admin_ops.php that contains an handle_upload function. That is just what I'm looking for.
So I created library/class.shopmadesimple_admin_ops.php (I already used a library folder, so didn't want to create
another similar folder).
In the actual program that should perform the upload I've included:

Code: Select all

if( !class_exists('shopmadesimple_admin_ops') ) {
	// This is required if we want to use the class
	$fn = cms_join_path(dirname(__FILE__),'library','class.shopmadesimple_admin_ops.php');
	require_once($fn);
}
$value = shopmadesimple_admin_ops::handle_upload($id,$error);
This results in: class 'shopmadesimple_admin_ops' not found.

Can someone explain to me how to implement (which things go were) lazy loading?

Thanks in advance,

Duketown

Re: Decreasing footprint of memory by using classes. But how

Posted: Mon Aug 15, 2011 3:14 pm
by tomgsd
I've been doing a similar thing with some modules I've been working on.

In order to make it work you have to have a "lib" folder in your module directory and put the classes in there. They are then picked up by the CMSMS autoloader (can be found in "CMS_ROOT/lib/autoload.php").

Alternatively, you can register your own autoload function in your MODULENAME.module.php file like this (I actually got this from the CGExtensions module before CMSMS had it's own autoloader):

Code: Select all

public function __construct(){
	parent::__construct();
	spl_autoload_register(array($this, 'autoload_classes'));
}

public function autoload_classes($classname){
	if(!is_object($this)) return;
	$fn = $this->GetModulePath()."/custom_lib_folder/class.{$classname}.php";
	if(file_exists($fn)){
		require_once($fn);
		return;
	}
}
Hope this helps!

Re: Decreasing footprint of memory by using classes. But how

Posted: Wed Sep 21, 2011 4:19 pm
by kidcardboard
Thx tomgsd for pointing that out. I've alway kept my classes in a dir called 'classes' and used my own autoloader. I'm gonna start using the CMSMS's autoloader function that I know it's there.