tags:

views:

159

answers:

7

I have learned how to use classes in PHP and so far I have only really found one useful application for them. I created a user class to preform various tasks relating to users in my web application such as output the avatar, show number of messages ect.

Aside from this example, what are some other useful ways to utilize classes in a practical sense?

+2  A: 

I use a database class all the time.

Here are a couple examples:

http://www.massless.org/_tests/phpdb/

http://slaout.linux62.org/php/index.html

http://www.tonymarston.net/php-mysql/databaseobjects.html

Mike B
A: 

I've built an utility class that humanizes the use of the mail() function, which I tend to use quite a lot.

Henrik Paul
Would you be able to share?
sirlancelot
Hm, I don't have the code at hand right now (a personal server I can't reach right now), but the idea is just to give convenient methods to create the "from"-headers, define a HTML-mail's MIME-headers, and such. Nothing advanced, just a small/quick conveniency/shorthand.
Henrik Paul
+2  A: 

It's a really good idea to read other people's code and see how they have separated things into classes. Look at some PEAR modules or a framework ( Zend, Symfony, Cake ).

too much php
A: 

By using classes (e.g. PEAR packages mentioned above, others), you are able to leverage code that has already been written and tested to perform common tasks.

Helps you to not reinvent the wheel, too.

barfoon
+1  A: 

Any time you can define a 'thing' that 'does stuff', you've got a candidate for defining an object. Two concrete examples, from the PHP standard library, that immediately pop to mind are :

  • Numerous database modules use objects for connections, queries & results.
  • The DateTime class encapsulates a generic concept of time with input & output formatting, timezone conversions & date arithmetic.

The thing is, Object Oriented Programming is a big idea - you can solve almost any programming problem in an object oriented way.

Sean McSomething
A: 

I would highly recommend learning about [design patterns][1]. Many of them make good use of classes and solve common programming problems. In particular the Factory and Abstract Factory patterns are a good place to start.

There is also an excellent book called PHP Hacks that has a chapter about implementing a host of different patterns in PHP, so you might want to check that out.

Also, explore some of these built-in objects in PHP to see how they work and get more ideas:

George Mandis
A: 

I created a user class to preform various tasks relating to users in my web application such as output the avatar, show number of messages ect.

Check out http://en.wikipedia.org/wiki/Cohesion_(computer_science)

Your example sounds like Logical cohesion.

Aim for functional cohesion. Each class does a particular task, and make classes as generic as possible and you'll find you can reuse them over and over.

A great example of that, is Symfony's sfParameterHolder:

http://www.symfony-project.org/book/1_2/02-Exploring-Symfony-s-Code#chapter_02_sub_parameter_holders

Symfony uses it to hold variables for view templates (in a MVC), to store request parameters in the web request object (itself a class that represents all request parameters dutifully stripped of backslashes etc), in the sfUser class to store all the parameters that eventually go in the $_SESSION etc etc.

Download Symfony's "sandbox", go into the /lib/symfony/ folder and learn from it. It"s complex but the code imho is very clean.

http://www.symfony-project.org/installation

Zend is nice too, but the number of include files mayb be overwhelming and I am personally not fond of their naming conventions, in particular using underscore prefixes.

faB