Best way of calling multiple classes?

Best way of calling multiple classes?

am 29.08.2007 01:44:42 von Phil Latio

I'm designing a small framework with a lot of classes (1 file per class) and
want to know the best method of calling each class?

Obviously I could I call each file that is used but that could be 10 or more
include statements. I am wonering if there is a better way? It's bit
annoying you can't build packages like Java.

Cheers

Phil

Re: Best way of calling multiple classes?

am 29.08.2007 01:53:21 von ivansanchez-alg

Phil Latio wrote:

> I'm designing a small framework with a lot of classes (1 file per class)
> and want to know the best method of calling each class?
>
> Obviously I could I call each file that is used but that could be 10 or
> more include statements. I am wonering if there is a better way? It's bit
> annoying you can't build packages like Java.

RTFM about __autoload().

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.22-1-amd64 kernel, KDE 3.5.7, and PHP
5.2.3-1+b1 generating this signature.
Uptime: 01:52:55 up 7 days, 22:13, 5 users, load average: 0.28, 0.31, 0.43

Re: Best way of calling multiple classes?

am 29.08.2007 02:39:29 von Jerry Stuckle

Phil Latio wrote:
> I'm designing a small framework with a lot of classes (1 file per class) and
> want to know the best method of calling each class?
>
> Obviously I could I call each file that is used but that could be 10 or more
> include statements. I am wonering if there is a better way? It's bit
> annoying you can't build packages like Java.
>
> Cheers
>
> Phil
>
>

I just put in the include statements.

Actually, I don't use include - I use require_once. And if a class
depends on another class (often in my case), the require_once goes in
the requiring file.

That way the main file only needs to worry about what it needs, as each
file takes care of its own includes.

__autoload is good - but it requires extra processing over and above a
require_once, and pretty much everything has to reside in the same
directory.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Best way of calling multiple classes?

am 29.08.2007 03:09:56 von Phil Latio

> I just put in the include statements.
>
> Actually, I don't use include - I use require_once. And if a class
> depends on another class (often in my case), the require_once goes in the
> requiring file.

This is what I doing now. I am just getting fed up with it.

> __autoload is good - but it requires extra processing over and above a
> require_once, and pretty much everything has to reside in the same
> directory.

I had heard of __autoload before but never realised what it did. I did a
Google after getting the other reply and came across
http://tobias-demuth.de/en/index.htm which looks interesting. Bit
frustrating that the author hasn't included some examples and a bit more
documentation because I don't fully understand how it supposed to work.

Cheers

Phil

Re: Best way of calling multiple classes?

am 29.08.2007 03:12:16 von Phil Latio

> RTFM about __autoload().

Thank you, I most certainly will.

Cheers

Phil

Re: Best way of calling multiple classes?

am 29.08.2007 03:21:20 von Michael Fesser

..oO(Jerry Stuckle)

>__autoload is good - but it requires extra processing over and above a
>require_once, and pretty much everything has to reside in the same
>directory.

My __autoload() invokes a special class loader method, which allows to
store all my classes and interfaces in subdirectories. The class loader
is able to recursively search through the tree structure if necessary.
The exact location of each found class is cached in an index file, so
the overhead for loading a class is quite small.

Micha

Re: Best way of calling multiple classes?

am 29.08.2007 04:15:20 von Jerry Stuckle

Michael Fesser wrote:
> .oO(Jerry Stuckle)
>
>> __autoload is good - but it requires extra processing over and above a
>> require_once, and pretty much everything has to reside in the same
>> directory.
>
> My __autoload() invokes a special class loader method, which allows to
> store all my classes and interfaces in subdirectories. The class loader
> is able to recursively search through the tree structure if necessary.
> The exact location of each found class is cached in an index file, so
> the overhead for loading a class is quite small.
>
> Micha

Not bad. But it also adds complexity and overhead.

Don't get me wrong - I like __autoload. But I also like to keep things
simple. Make for much easier troubleshooting in six months.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Best way of calling multiple classes?

am 29.08.2007 10:06:02 von gosha bine

On 29.08.2007 03:09 Phil Latio wrote:
>> I just put in the include statements.
>>
>> Actually, I don't use include - I use require_once. And if a class
>> depends on another class (often in my case), the require_once goes in the
>> requiring file.
>
> This is what I doing now. I am just getting fed up with it.

how about

foreach(glob("lib/*.php") as $file) require_once($file)


>
>> __autoload is good - but it requires extra processing over and above a
>> require_once, and pretty much everything has to reside in the same
>> directory.
>
> I had heard of __autoload before but never realised what it did. I did a
> Google after getting the other reply and came across
> http://tobias-demuth.de/en/index.htm which looks interesting. Bit
> frustrating that the author hasn't included some examples and a bit more
> documentation because I don't fully understand how it supposed to work.
>

autoload is rather a dirty hack and as such should be avoided.

--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi

Re: Best way of calling multiple classes?

am 29.08.2007 11:25:50 von Michael Fesser

..oO(Phil Latio)

>I had heard of __autoload before but never realised what it did. I did a
>Google after getting the other reply and came across
>http://tobias-demuth.de/en/index.htm which looks interesting. Bit
>frustrating that the author hasn't included some examples and a bit more
>documentation because I don't fully understand how it supposed to work.

I had a look at the code - I wouldn't use it. The code is ugly and has a
really big bug: The function that searches the class tree is called on
every __autoload() call, which pretty much defeats the purpose of having
a cache file. It's easy to fix (just a missing 'else'), but nevertheless
that shouldn't happen.

Micha

Re: Best way of calling multiple classes?

am 29.08.2007 11:25:50 von Michael Fesser

..oO(Jerry Stuckle)

>Not bad. But it also adds complexity and overhead.

Yep, but it pays off for me. I profile my code from time to time and the
real bottlenecks are always somewhere else.

>Don't get me wrong - I like __autoload. But I also like to keep things
>simple. Make for much easier troubleshooting in six months.

Point taken, but for me it has real advantages. My entire framework has
become rather complex and is still growing. I also have to change the
directory structure from time to time to clean things up (there are
still some ugly things left from the early days of that class library,
which have to be improved). So it makes things a lot easier if I can let
the library perform some rather common tasks automatically.

A while ago I also thought about something like an import() function,
which could have worked like the 'import' statement in Java:

import('/path/to/classes/TFoo'); // load a single class
import('/path/to/classes/*'); // load all classes in that directory

But then PHP 5 and __autoload() came up ...

Another interesting approach I once saw (IIRC in the Prado framework,
but I'm not sure) was something like the import() above, but the
function didn't load the classes, but just added the supplied path to
PHP's include_path. Then the class could easily require_once all other
required classes without having to worry about the directory structure.

Micha

Re: Best way of calling multiple classes?

am 29.08.2007 16:43:11 von Phil Latio

"Michael Fesser" wrote in message
news:cth9d3h6n9a5an6jtcvsj75g2ok8jk5gvo@4ax.com...
> .oO(Jerry Stuckle)
>
>>__autoload is good - but it requires extra processing over and above a
>>require_once, and pretty much everything has to reside in the same
>>directory.
>
> My __autoload() invokes a special class loader method, which allows to
> store all my classes and interfaces in subdirectories. The class loader
> is able to recursively search through the tree structure if necessary.
> The exact location of each found class is cached in an index file, so
> the overhead for loading a class is quite small.

Did you write this yourself did you download it from somewhere? Sounds like
just what I am after.

Cheers

Phil

Re: Best way of calling multiple classes?

am 29.08.2007 20:03:26 von zeldorblat

On Aug 29, 4:06 am, gosha bine wrote:
>
> autoload is rather a dirty hack and as such should be avoided.
>

Why do so many people say this? autoload does exactly what it was
intended to do and it works perfectly. It solved a major problem that
people had complained about for a long time. Would you care to
elaborate on your objections to using it?

Re: Best way of calling multiple classes?

am 29.08.2007 22:37:31 von gosha bine

ZeldorBlat wrote:
> On Aug 29, 4:06 am, gosha bine wrote:
>> autoload is rather a dirty hack and as such should be avoided.
>>
>
> Why do so many people say this? autoload does exactly what it was
> intended to do and it works perfectly. It solved a major problem that
> people had complained about for a long time. Would you care to
> elaborate on your objections to using it?
>

autoload is a "magic quotes"-type of solution to me: it attempts to
solve the real problem, but at the wrong place and in the wrong way.
Besides that, it's "magic" (which stinks like hell) and it's global
(which stinks even more).

We don't need yet another stinky "magic" quick fix in php. We do need a
decent packaging system and the catchable ClassNotFound exception.


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok

Re: Best way of calling multiple classes?

am 29.08.2007 23:01:31 von Bucky Kaufman

gosha bine wrote:

> autoload is a "magic quotes"-type of solution to me: it attempts to
> solve the real problem, but at the wrong place and in the wrong way.
> Besides that, it's "magic" (which stinks like hell) and it's global
> (which stinks even more).
>
> We don't need yet another stinky "magic" quick fix in php. We do need a
> decent packaging system and the catchable ClassNotFound exception.

I gotta say - that whole "magic" thing really turns me off. I'm sure
someone somewhere has good use for it - but as an engineering type, I
get real nervous when folks start naming their algorithms after concepts
from fantasy novels.

I just seems so... deprecable.

(And if I see one more security server named "gandalf", I swear to God
I'll put on a suicide vest and blow the damned thing up myself. - Just
kidding - hahahaha.)

Re: Best way of calling multiple classes?

am 30.08.2007 00:41:49 von Michael Fesser

..oO(gosha bine)

>autoload is a "magic quotes"-type of solution to me: it attempts to
>solve the real problem, but at the wrong place and in the wrong way.
>Besides that, it's "magic" (which stinks like hell)

__set(), __get(), __call() ... all "magic", but very useful from time to
time. If you don't like them, simply don't use them.

Micha