using PHP classes ... when?

This is a discussion on using PHP classes ... when? within the PHP Language forums, part of the PHP Programming Forums category; Hi there, I have been using PHP for a few years but never used classes. I understand how they are ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-10-2004
bissatch@yahoo.co.uk
 
Posts: n/a
Default using PHP classes ... when?

Hi there,

I have been using PHP for a few years but never used classes. I
understand how they are created and how they work but was unsure about
when you use them.

Any help? Cheers

Burnsy

Reply With Quote
  #2 (permalink)  
Old 12-10-2004
Rick van Krevelen
 
Posts: n/a
Default Re: using PHP classes ... when?

> I have been using PHP for a few years but never used classes. I
> understand how they are created and how they work but was unsure about
> when you use them.


Classes are basically constructions of variables with some specific
functions added to them. Organizing your data in this way greatly improves
reuse of code and readibility (if applied properly). I suggest you visit the
free repository http://phpclasses.org where all of PHP's possibilities are
exemplified in classes complete with example scripts.


Reply With Quote
  #3 (permalink)  
Old 12-10-2004
Erwin Moller
 
Posts: n/a
Default Re: using PHP classes ... when?

bissatch@yahoo.co.uk wrote:

> Hi there,
>
> I have been using PHP for a few years but never used classes. I
> understand how they are created and how they work but was unsure about
> when you use them.
>
> Any help? Cheers
>
> Burnsy


Hi Burnsy,

Classes/Objects are a great way to bundle some functionality in a ordered
way.
You don't have to worry about namespaces and such.
A good designed class will expose its functionality and you don't have to
worry each time you use it HOW that functionality is excactly implemented.

Great for reuse of functionality/code.

Another good thing is the fact that a class can hold its own set of internal
variables. Unlike a function, which forgets it variables when finished, an
object is more persistent and will remember all important variables for its
functioning. (Untill you destroy it of course)

Of course it is perfectly possible to screw things up with a class too. :P

Regards,
Erwin Moller
Reply With Quote
  #4 (permalink)  
Old 12-10-2004
Tony Marston
 
Posts: n/a
Default Re: using PHP classes ... when?

If you want to download a complete sample application that uses class then
take a look at http://www.tonymarston.net/php-mysql...plication.html
There are plenty of other articles on my site which give info on OOP.

--
Tony Marston

http://www.tonymarston.net



<bissatch@yahoo.co.uk> wrote in message
news:1102676617.049332.57740@c13g2000cwb.googlegro ups.com...
> Hi there,
>
> I have been using PHP for a few years but never used classes. I
> understand how they are created and how they work but was unsure about
> when you use them.
>
> Any help? Cheers
>
> Burnsy
>



Reply With Quote
  #5 (permalink)  
Old 12-10-2004
Gary L. Burnore
 
Posts: n/a
Default Re: using PHP classes ... when?

On Fri, 10 Dec 2004 18:32:39 -0000, "Tony Marston"
<tony@NOSPAM.demon.co.uk> top posted like a moron and wrote:

>If you want to


read advertisments, I'll by a newspaper.

--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Reply With Quote
  #6 (permalink)  
Old 12-11-2004
Chung Leong
 
Posts: n/a
Default Re: using PHP classes ... when?

<bissatch@yahoo.co.uk> wrote in message
news:1102676617.049332.57740@c13g2000cwb.googlegro ups.com...
> Hi there,
>
> I have been using PHP for a few years but never used classes. I
> understand how they are created and how they work but was unsure about
> when you use them.
>
> Any help? Cheers
>
> Burnsy
>


Well, if you haven't use classes then you probably don't need them for what
you're doing. Usually it's self-evident when using a class is appropriate.
In the case of basic web application development in PHP, there aren't that
many instances where it brings much benefit.

Objects are good for managing states. For example, I like to use objects to
hold data coming from the database. Fields are exposed as object properties
which my application code can freely get and set. Each object internally
keeps copies of the original values, so that when it comes time to save the
data back to the database I can easily tell which fields have changed and
which have not.

A crude example:

class User {
var $user_id;
var $user_name;
var $first_name;
var $last_name;

var $org_user_name;
var $org_first_name;
var $org_last_name;

function User($row = false) {
if($row) {
$this->user_id = $row['user_pk'];
$this->first_name = $this->org_first_name = $row['first_name']
// ... and so on
}
}

function Save() {
if(!$this->user_id) {
// no primary key, meaning we need to do an insert
}
else {
if($this->first_name != $this->org_first_name) {
// add to update list
}
// ... and so on
}
}
}

Situations where a PHP script need to remember state information don't
happen very often though. Most of the time things are calculated, echo'ed to
the client, and quickly discarded. Simple functions serve you well enough.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0