RE: Classes - Dumb question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 2007-10-11 at 07:36 -0500, 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
> 	 */
> }
> 
> http://www.sitepoint.com/article/object-oriented-php

Another good reason is for function/data grouping. By using a class you
don't have to worry about name collisions for your functions, although
you can still have class name collisions. Also, data related to any
operations can be tracked via the object rather than cluttering the
global namespace between function calls.

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux