At 7:36 AM -0500 10/11/07, Jay Blanchard wrote:
[snip]
okay, this is really (!) embarassing, but I have to ask:
Why would I want to use classes in PHP?
I have been using PHP for years now and writing the "normal" functions all
the time. I have never even bothered working with classes, but now I would
love to know what makes the classes so special...
Please go easy on me ;o) Just trying to make another step :o)
[/snip]
Do not be embarrassed, this is a very good question.
First of all what you call "normal" is procedural or functional
programming. There is nothing wrong with doing things this way and
may be especially quick and efficient when doing basic web sites and
applications. Document well and you will have no problem maintaining
your code.
OOP (object oriented programming) is especially useful when the
application you have created needs to scale. A quick example; you
have sold your products to the consumer market for a long time but
now the commercial market has become interested. Commercial
customers are different than non-commercial customers, different
data, different credit requirements, different shipping, etc. but
they still have a lot in common, If you had a class Customer you
could extended that class to include commercial customers and only
have to code for the unique qualities of that kind of customer. Then
if another type of customer crops up, say a military contract, you
could extend again;
class Customer {
....
}
class CommercialCustomer extends Customer {
/*
*only code unique to commercial customers
* inherits from Customer other variables
* and functions that are common
*/
}
class MilitaryCustomer extends Customer {
/*
*only code unique to military customers
* inherits from Customer other variables
* and functions that are common
*/
}
Jay:
Yes, but I could do that procedurally from within the customer
function by simply adding a customer type (needed regardless) and
using a switch to direct and collect the additional data needed.
function customer($whatWas, $customertype, $whatAdditional)
{
/* do "what was" (i.e., common to all) */
/* then do what's additional unique to type */
switch(1)
{
case $customertype =='Commercial':
commercialCustomer($whatAdditional);
break;
.. and so on
}
function commercialCustomer($whatAdditional)
{
/*
*only code unique to commercial customers
*/
}
function militaryCustomer($whatAdditional)
{
/*
*only code unique to military customers
*/
}
In either case, I still have to write more code to accommodate
scaling. And, if I have more customer types, then it's a simple
matter to add more customer functions and addition case statements to
the initial customer function. I don't see the benefit in using a
class. At this point, it just looks like a different way of doing
things.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php