Marcus Bointon wrote:
On 1 Jun 2005, at 09:01, janbro wrote:
require (Class2.php);
I bet you didn't cut'n'paste that from a working file :-)
class Class1{
private function ... {
$refClass2 = new Class2;
}
}
Now my question, is it possible to skip that require/ include part? In
Java you don't need that, as class and file names are identical.
PHP doesn't have this luxury - people can call files whatever they
like. However, PHP5 does have a nice feature to deal with this. In your
class1 class, create a function called __autoload like this:
function __autoload($class_name) {
require_once $class_name . '.php';
}
In this case when you ask for a new class2 and you've not required it
before, it will automatically call __autoload with $class_name set to
'Class2', which then requires the class file according to the pattern
used in the function, in this case 'Class2.php'. Note that while PHP is
not case sensitive to class names, the file system you're on probably
is, so keep your case consistent throughout.
all true, now imagine that you install a couple of 3rdparty php5 'packages'
and they all define __autoload() - ain't gonna work! which is why there has been
discussion on internals regarding the possibility of use a handler-function stack
for autoloading (in the same way that you can register a stack of input/output
filter-function)... something to keep an eye on in case things change :-)
you may also consider that placing suitable require statements (I would
use require_once for class files) before a class definition will
probably/possibly (I don't know but it may be important to your project)
be a minor performance boost over letting php call _autoload() whenever it
needs.
Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php
Marcus
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php