473,396 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 17 '05 #1
5 1542
> 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.
Jul 17 '05 #2
bi******@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
Jul 17 '05 #3
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

<bi******@yahoo.co.uk> wrote in message
news:11*********************@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

Jul 17 '05 #4
On Fri, 10 Dec 2004 18:32:39 -0000, "Tony Marston"
<to**@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
================================================== =========================
Jul 17 '05 #5
<bi******@yahoo.co.uk> wrote in message
news:11*********************@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.
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: nospam | last post by:
I got three (3) files (1) Untitled.aspx (2) Untitled.aspx.1.cs (3) Untitled.aspx.2.cs These three files must be used together to make file #1, Untitled.aspx, page work via J.I.T. when the...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
6
by: Robert | last post by:
Hello. I have been trying out the Lebans ToolTip Classes at http://www.lebans.com/tooltip.htm, to display "balloon" style help tips in a form. The classes I am using are located at...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
1
by: Ankur Goenka | last post by:
Hi, I am trying to include two proxy classes (from two different web services say Namespace.Service1 and Namespace.Service2) in a project (C# Class lib). These webservice internally use (as...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.