|
Posted by Matthew Weier O'Phinney on 06/01/05 23:26
* janbro <janbro@web.de> :
> Okay, I see there is a workaround but nothing realy satisfying, but I'm
> not hoing to complain, I'm hapy to have oo in PHP at all. I never worked
> with __autoload so far, but I will give it a try and check it out
The best way to use autoload is with the auto_prepend_file configuration
directive. By adding the __autoload() function to a file that is
automatically prepended, you don't even have to think about it in your
scripts.
This kind of flexibility is nice -- you can have different prepend files
for different sites or different areas of the same site -- so that you
only have the funcationality accessible that you need right there. You
can also define your own naming schema -- which, admittedly is a dubious
advantage, but an advantage nonetheless.
> Marcus Bointon schrieb:
> > On 1 Jun 2005, at 09:01, janbro wrote:
> >
> > > require (Class2.php);
> > > 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.
> >
> > Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php
--
Matthew Weier O'Phinney | WEBSITES:
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:matthew@garden.org | http://vermontbotanical.org
Navigation:
[Reply to this message]
|