RE: [PHP] Classes - Dumb question



[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
.



Relevant Pages

  • Re: e-commerce portal
    ... something like a LAMP solution is how our ordering and billing system ... customer number and a contact account. ... It should be easy to find PHP developers to work on the front-end. ... for data but subsequent requests for the same dropdown/grid lists come ...
    (comp.databases.pick)
  • Re: How to limit access by customer ref?
    ... simply enter their alphanumeric customer reference to access the page. ... Ideally this code would then be added to the bank payment form that they ... with access to cgi and php. ... programming background so I could pick up php or cgi if I knew where to ...
    (comp.infosystems.www.servers.unix)
  • Re: Ruby vs. PHP
    ... to push his custom Ruby solution over a custom PHP ... The reasoning behind building another Ruby Web Framework from ... a Warts-n-all PHP solution may have suited the customer better ...
    (comp.lang.ruby)
  • RE: Access denied for user root@localhost (using password: NO)
    ... >> Do you run php database driven apps on the same server as you use to ... are selling/giving/whatever customer access to php on this server, ...
    (freebsd-questions)
  • Re: Ruby vs. PHP
    ... to push his custom Ruby solution over a custom PHP ... a Warts-n-all PHP solution may have suited the customer better ... PHP is more popular, so migrating from one shared host to another is ...
    (comp.lang.ruby)