For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > December 2007 > storing php5 classes and function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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!

macca

2007-12-22, 7:03 pm

Take a look at autoloading.

http://uk2.php.net/autoload

e.g.

function __autoload($class_name) {
require_once $class_name . '.php';
}
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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com