| Author |
storing php5 classes and function
|
|
| Alamin Ahmed 2007-12-22, 7:03 pm |
| 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!
| |
|
|
| Dikkie Dik 2007-12-23, 8:03 am |
| > 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.
| |
| Ulf Kadner 2007-12-24, 8:02 am |
| 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
|
|
|
|