Re: Organisation of classes
| From: | Matthew Weier O'Phinney | Date: | Thu, 02 Dec 2004 03:17:53 +0000 |
| Subject: | Re: Organisation of classes | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-203307@lists.php.net to get a copy of this message | ||
* Peter Lauri <peter@lauri.se>:
> I have considered that, but I do not like that idéa :) I want php to
> recognise what classes it needs to load. Have there been any improvment in
> this area to PHP5? I use PHP4 for the moment, but will migrate to PHP5 soon.
Yes. In PHP5, you can register an '__autoload()' function at the
beginning of your script, and in there map class names to files. To
further automate things, you could always put the __autoload() function
into a file and then set the php.ini 'auto_prepend_file' directive to
load this file -- that way you wouldn't need to worry about defining it
at the beginning of each script.
> As an "old" java programmer I feel that PHP is not that well deveolped. Was
> hoping that it would be some sort of library system that could be used for
> classes. Package?
PHP and Java are very different beasts. PHP is dynamically typed and has
a very flexible, dynamic runtime environment. One reason is to allow
scripts to define their own environment -- doing so makes it very easy
to drastically change the behaviour of a script simply by placing it in
a different location. (This can also open a can of worms, but if
developed right, this can be very powerful.)
However, if you're looking for consistency of behaviour, it's possible
to do that with PHP as well; you simply have to be a little more
rigorous about setting up your environment.
> "John Holmes" <holmes072000@charter.net> skrev i meddelandet
> news:41AE81DB.4070200@charter.net...
> > Peter Lauri wrote:
> > > For the moment I have all my classes saved in a file called
> > > classes.php in a subdirectory /classes/. I have started to get to
> > > many classes in the same file. In java I do this the default way,
> > > just naming them myclass.class. Is there a similar way to do this
> > > so that I do not need to include every file as a .php class in
> > > every php that I want to use the class?
> >
> > Save each class into its own name.class.php file.
> >
> > As for the includes, if there are a lot of classes and you actually
> > need _all_ of them loaded, then just make one "include.php" that
> > includes all of the class files and then just add
> > "include('include.php')" into each script that needs these classes.
> >
> > You should really take a look at what classes need to be loaded in
> > each page, though. If you have 10 classes, but a page only uses 3 of
> > them, then it's a waste of time to load those other 7 classes.
> > That's basically what you're doing now with everything in one file.
> >
> > Think about using extra classes that load the others that are needed
> > for specific functios. Load a "show" class that'll load 4 of the 10
> > classes needed for showing records, load an "edit" class that'll
> > load 7 of the 10 classes needed for editing, etc...
--
Matthew Weier O'Phinney | mailto:matthew@garden.org
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org