storing php5 classes and function

storing php5 classes and function

am 22.12.2007 20:22:30 von Alamin Ahmed

what is the best way to store and call php classes written and
submitted by other developers?

let's say i want to do the following

$obj = new $userclass();
$obj->display($assoc_array);

basically developers will have away to write different "print" method
of given array. How and where would I store their class source fine
and how would I load and use to display?

Thanks!

Re: storing php5 classes and function

am 22.12.2007 21:14:53 von Macca

Take a look at autoloading.

http://uk2.php.net/autoload

e.g.

function __autoload($class_name) {
require_once $class_name . '.php';
}

Re: storing php5 classes and function

am 23.12.2007 11:23:58 von Dikkie Dik

> function __autoload($class_name) {
> require_once $class_name . '.php';
> }

One nice thing to notice: an included file can return a value, which may
be an instance. So you could write a class in a file that looks like this:

class WhatEver
{
....
}

return new WhatEver();

And then call that dynamically:

$obj = require($pathToWhatEver);

I use this trick for my unit testing system.

Re: storing php5 classes and function

am 24.12.2007 11:44:08 von Ulf Kadner

macca schrieb:
> Take a look at autoloading.
>
> http://uk2.php.net/autoload
>
> e.g.
>
> function __autoload($class_name) {
> require_once $class_name . '.php';
> }

Thats the bad way.

Better to use spl_register_autoload() with userdefined autoload
functions. So other developers are also able to use own autoloads.

So long, Ulf