|
Posted by Jerry Stuckle on 05/10/05 05:39
bissatch@yahoo.co.uk wrote:
> Hi,
>
> Generally if I re-use code, I use a function. If I need to use these
> functions over a number of pages I write the function to an include
> file where all pages have access.
>
> So when should I ever use PHP classes instead. I have learned how to
> put together PHP classes but never seen a reason to use them where I
> can simply use a function.
>
> Im basically just wanting to know classes benefits over functions. If
> something can be better put together in a class I would like to be
> doing it that way. Any suggestions?
>
> Cheers
>
> Burnsy
>
As Jacob said, this all goes back to whether Object Oriented programming
is "better" than structured programming.
Basically - variables have states (hold information). Functions have
behavior (do things). Objects (the instantiation of classes in PHP)
have both.
Classes have an additional feature, though. They can abstract the data.
For instance - I create a lot of database classes. It doesn't take
long, but it has advantages. For instance - due to a change in
requirements on one site recently, I had to split a table. Basically, I
had the original table minus a little data. That missing data went into
a second table. A third table provided linkage between the other two.
There were > 200 pages dependent at least in part on this table. How
many needed to be changed? Zero. Everything was in a class; I modified
the class to handle three tables instead of one. I had to change two
administrative pages which were the actual cause of the change. But
everything else was OK.
Generally, writing OO code does take longer than non-OO code, just as
writing functions takes longer than doing something inline. The
advantage come when you reuse the code, or, as in above, need to make
changes to the back end.
OO is not the best in every circumstance. But it has its advantages.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|