Trading Systems Analyst (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Classes and PHP
The hardest concept I've tried to understand since beginning to use PHP was that of classes. I'd never used a database engine but learning to use MySQL, at least for the more basic functions, was a breeze. Having never used OOP before, classes were novel as well, but understanding the theory and why it was useful escaped me. I knew they must be powerful as "everything" is programmed using OOP, but for the life of me, although I thought I understood the mechanics, I couldn't see the usefulness. Then, just a few days ago, while trying to figure out how to do something with regular functions it hit me just how doing it with objects would make my job much simpler! I'm going to try to explain about them in plain English and hopefully help others like myself.
Classes are nothing more than a collection of variables and functions acting on those variables. They provide a means of thinking about things in real world terms. In other words they describe an object. An object, or instance, of a class is an actual "living, breathing" structure of that class. Let's say we want to describe a bicycle. A proper class of a bicycle might have the variables $pedals, $chain, $front wheel, $rear wheel, $brakes, and $handle_bars. Functions of the bicycle would include Stop(), Accelerate(), Coast(), TurnLeft() and TurnRight(). You can think of your script as the entity operating that bike. The function Accelerate() could be passed an argument such as $Braking_Force and use that information along with the defined instance variables (probably $brakes and $wheels) and output some result back to your script.
Interesting, but couldn't all this be accomplished using regular variables and functions? Yes it could, and if you only had one bike in your script it probably wouldn't make much sense to define a class just for it. But what if you needed several bicycles? It could become quite complex keeping track of all those variables and making sure you pass the correct variables to the different functions, and using objects cuts down on the number of variables you need to pass because the function automatically has available to it all the variables describing the object the function is acting upon. Also, class definitions are easily included in different scripts and you'll be assured that a bicycle works the same way in each script!
Let's create a class that I actually use on almost every page on my site and you might find useful, too.
I don't know about you, but when I'm writing a dynamic web page I hate to have to stop thinking about the logical flow to worry about properly formatting my HTML. As a consequence of this, I often end up with not so attractive pages because I don't want to worry about font faces and sizes, or background and text colors. The solution: using a PHP class to set HTML output attributes with functions to format the text!
I call the class "Style". It contains the following variables that set important HTML attributes for formatting the output:

<?php

class Style {

    var
$text;
    var
$alink;
    var
$vlink;
    var
$link;
    var
$bgcol;
    var
$face;
    var
$size;
    var
$align;
    var
$valign;

}

?>
I'm sure you're familiar with HTML so the variables should be self- explanatory. Next I created a function for Style called Style:

<?php

class Style {

function
Style ($text="#000000",$alink="#AA00AA",
$vlink="#AA00AA",$link="#3333FF",
$bgcol="#999999",$face="Book Antiqua",$size=3,$align="CENTER",$valign="TOP") {

    
$this->text=$text;
    
$this->alink=$alink;
    
$this->vlink=$vlink;
    
$this->link=$link;
    
$this->bgcol=$bgcol;
    
$this->face=$face;
    
$this->size=$size;
    
$this->align=$align;
    
$this->valign=$valign;

}

}
?>
[ Next Page ]


Comments:
RE: Why use classes?Reynolds Design08/19/09 09:57
Great keep it upwaqar01/30/09 12:50
php class Vs cssSte12/06/08 04:42
Good PHP and Classes MaterialRamesh11/13/08 08:15
RE: Why use classes?Ray06/02/08 18:50
RE: Why use classes?Harish Kumar05/27/08 09:49
where to store the classescml05/15/08 14:46
Classes and PHP: a Study in Active ReadingJ. S.10/18/07 19:07
OOP templatesregin 05/06/07 03:10
Different Between overloading & OverridingRazzaq04/10/07 12:48
Detect Code information KomputerXv3n0m10/14/05 22:29
it's so sosuperthaix04/30/04 13:10
RE: Heard of CSS?Lol03/24/04 07:43
RE: Great workBodzan03/07/04 20:42
Great workDave02/09/04 06:10
thanks rodcOdepEcker01/12/04 02:14
Thanks alotX-Treme12/23/03 08:31
RE: Heard of CSS?Mike10/04/03 18:56
Good Job!Fire_Code09/20/03 16:34
RE: Heard of CSS? DUH!Osman Khoury07/26/03 10:15
RE: HOW THE F*CK DO I MAKE A FORUM ON MY WEBPAGE?zach05/07/03 22:18
RE: HOW THE F*CK DO I MAKE A FORUM ON MY WEBPAGE?david03/28/03 16:18
RE: Why use classes?Jeroen Keppens03/04/03 08:21
RE: Why use classes?nip02/24/03 15:45
RE: HOW THE F*CK DO I MAKE A FORUM ON MY WEBPAGE?nip02/24/03 15:39
Why use classes?noel darlow02/10/03 20:04
Sams Publishing "PHP and MySql"jonny02/04/03 01:59
RE: HOW THE F*CK DO I MAKE A FORUM ON MY WEBPAGE?Teemaz01/12/03 10:24
some error debugging....Miss Smita Hagavane01/03/03 02:22
RE: Can not instantiate the classDa Phreeek01/01/03 00:48
RE: HOW THE F*CK DO I MAKE A FORUM ON MY WEBPAGE?Aaron Paxson12/30/02 13:54
RE: Where do classes go?steve12/19/02 12:45
RE: Heard of CSS? DUH!sasha12/03/02 13:34
Where do classes go?Keith11/29/02 12:40
RE: Heard of CSS? DUH!phil11/23/02 11:45
good use for classesDavid Kelley11/10/02 12:40
HOW THE F*CK DO I MAKE A FORUM ON MY WEBPAGE?Bobby Charlton11/05/02 21:07
RE: no much help , just wasted timesabado11/04/02 07:18
RE: no much help , just wasted timeR Pagels10/20/02 10:07
RE: no much help , just wasted timeEvert10/18/02 12:20
no much help , just wasted timeditzi10/14/02 10:07
RE: Escape quoteMatthew09/10/02 11:10
RE: what is this error message!!!Matthew09/10/02 10:52
RE: what is this error message!!!Bram Vaessen09/09/02 15:20
RE: Question about Class designSankaranarayanan09/06/02 14:11
what is this error message!!!Diego09/02/02 10:05
Can not instantiate the classChirag Bhatt08/30/02 05:25
RE: undefined function: set() mak08/19/02 04:02
RE: This is great programming but...Adam08/12/02 04:34
RE: OO to the limit...& with structureAdam08/12/02 04:17
RE: What is "this"?Adam08/12/02 04:15
Escape quoteBill McCarthy08/09/02 10:04
Mailing List Using PHPRaj08/04/02 20:44
RE: Formatting php resultsAlex07/18/02 02:56
RE: Drop Down menusDante07/15/02 12:56
Formatting php resultsRahul07/15/02 09:46
How to simulate class variablesHenk07/14/02 05:56
Sub-classes ?!josé moreira07/08/02 10:32
undefined function: set() damagic06/17/02 21:18
How do I make forums??Hallgeir Øgaard06/06/02 02:41
RE: Drop Down menusDogdac06/05/02 16:23
ClassLinda06/05/02 01:53
Drop Down menusshamik joshi06/03/02 15:06
RE: Object 'chains'rajka05/23/02 09:36
RE: Object 'chains'rajka05/23/02 09:32
structure in PHPkhairuddin05/14/02 05:23
destructorsMartin05/07/02 07:23
RE: why use setters?morgan04/16/02 12:02
Class, Objects & FunctionsVampire Du Cross ^v^04/15/02 19:41
why use setters?Olenie04/10/02 07:39
RE: Getting started with php and mysqlMarc04/05/02 07:41
default parametersHStoddard03/22/02 03:17
prototyping functions after class instantiatiIan Ferger03/18/02 20:12
passing db connect to the sessionKhanh 03/04/02 23:54
pass db connection handle to sessionKhanh 03/04/02 23:35
RE: The complete class and an example scriptDeborah Cole02/22/02 08:51
thank-you!Deborah Cole02/22/02 08:41
RE: what are the backslashes for? Isaac02/01/02 15:19
Getting started with php and mysqlMags01/30/02 08:51
What's news about Php ClassBen Thaier Malik01/30/02 02:12
what are the backslashes for? David Fields01/28/02 20:13
RE: Heard of CSS?Cory01/11/02 19:12
How to read/write class objects in a file.deepak01/08/02 21:55
RE: Heard of CSS?HotMetal01/05/02 15:06
RE: Why not use CSS?HotMetal01/05/02 05:05
RE: database connection in a functionlowrykun01/01/02 14:42
database connection in a functionLuke12/30/01 21:58
RE: how php with classes challenge javapenguin12/17/01 15:00
need modul php4.0-interbase6.0budi12/03/01 05:33
RE: Why not use CSS?Piem11/27/01 21:12
The complete class and an example scriptJerome Schellie11/24/01 15:09
THANK YOU!Jerome Schellie11/24/01 14:56
RE: Why not use CSS?bungatron11/18/01 12:42
Why not use CSS?SlaveX11/10/01 05:02
RE: integer formattingCarlos Viana11/07/01 06:08
integer formattingLuke11/06/01 01:24
How to get the name of object variable???Carlos Viana10/31/01 09:59
mysql_select_db functions in classesPhil10/30/01 10:56
RE: Object 'chains'Andrew10/29/01 15:46
RE: Objects and SessionsJoris Kluivers10/27/01 05:10
RE: What is "this"?Alessandro Bellina10/24/01 23:06
RE: Objects and Sessionsrod k10/15/01 07:33
RE: assigning membersrod k10/15/01 07:31
RE: classes and sessionrod k10/15/01 07:26
Objects and SessionsKoby Leung10/09/01 18:23
RE: Drop Down menusAndreas Adler09/21/01 17:25
Drop Down menusLuke09/17/01 05:35
assigning membersJack Stinch09/15/01 20:28
RE: Action variable help!!!!!!Stk09/14/01 05:20
Action variable help!!!!!!Sue09/11/01 13:58
Add action variable Help!!!!!!!!!!!!!!!!!!!!!Sue09/11/01 13:57
RE: What is "this"?|Laz|09/06/01 12:39
RE: Accessing functions within a class Bill Toffel09/04/01 06:58
Accessing functions within a class Steve09/01/01 06:26
RE: What is "this"?Sam Aronoff08/23/01 12:16
OO VariablesSam Aronoff08/23/01 12:10
Heard of CSS?Brian Smith08/08/01 09:55
classes and sessionnocontrol07/26/01 05:56
php and mssql2000 server connectedMizanur Rahman07/06/01 23:22
trouble with TROUT-rewrote functionCraig06/20/01 10:47
RE: Object 'chains'Peter06/20/01 03:04
RE: counterJesse F. Josserand06/14/01 23:38
RE: counterGalen06/13/01 19:10
RE: Object 'chains'Jörgen Lindholm05/30/01 04:34
Your instructive document on PHP ClassesJesse F. Josserand05/27/01 23:39
RE: how php with classes challenge javaJeremy05/19/01 10:54
counterNeil05/03/01 02:14
I really appreciate your documentNainil Chheda04/25/01 11:32
Class variablesWellington04/20/01 12:34
How about Class variables?Wellington04/19/01 20:38
Additions?Danny Tuppeny04/18/01 10:30
RE: how php with classes challenge javaArta04/17/01 05:10
private functions???Joost04/13/01 09:32
RE: CounterElBasta04/05/01 23:23
inheritanceHarnesk04/02/01 19:04
RE: Object 'chains'Mirian Kakulia04/02/01 18:30
Need to install a library for PHPEsteban Cazarez03/29/01 17:55
RE: This is great programming but...Joost03/29/01 06:19
RE: How to upload file from local pc to server?ThePhobos03/28/01 15:37
How to upload file from local pc to server?sayma03/27/01 02:25
ThanxDaniel03/20/01 12:53
RE: copy/paste warning - easy solutionMacgruder03/09/01 10:48
RE: how php with classes challenge javarod k03/03/01 05:51
how php with classes challenge javahemant pardesi03/02/01 02:54
Excellent!Richard Sanders02/28/01 12:38
Here's the complete code in orderRodney Hampton01/29/01 09:35
RE: functions returning valuesrod k01/25/01 15:33
objects and arrays in php4Heiko Müller01/25/01 14:04
functions returning valuesemily01/25/01 11:13
RE: Overloading in PHProd k01/15/01 09:15
RE: This is great programming but...rod k01/15/01 09:14
copy/paste warningNils01/14/01 18:36
Overloading in PHPApril Harry Quinones01/11/01 01:11
OO to the limit...& with structureandrezero01/08/01 13:27
RE: Object 'chains'DarthGrobi01/05/01 09:52
RE: Question about search codingGreg Hazel01/04/01 14:47
RE: Where is the restRichard 12/18/00 14:32
RE: Are there PDF`s???Aida12/13/00 03:33
Are there PDF`s???Vicente Ortega12/12/00 00:13
Where is the restSulaiman12/10/00 21:15
RE: Question about FunctionsRobert Treat12/01/00 22:14
Question about FunctionsStuart Barker12/01/00 10:01
Object 'chains'Wellington11/28/00 17:16
RE: This is great programming but...Joel Allen11/24/00 14:07
This is great programming but...Kina Dazegh11/22/00 12:32
CounterVicente11/22/00 08:22
RE: What is "this"?John Barker11/19/00 00:07
RE: Question about search codingkaren11/15/00 20:39
RE: Question about declaring variablesRod Kreisler11/11/00 15:11
Great class example - just summarize itHardy Merrill11/03/00 09:33
Question about declaring variablesAnatole10/24/00 03:13
Yet again...thanksChad10/19/00 13:14
-==Send Data==-LUis Augusto10/16/00 09:23
ThanksMuhaha10/15/00 00:53
Many ThanksDon10/13/00 00:48
RE: What is "this"?Karl Trygve Kalleberg07/09/00 13:22
I finally got the TABLE Part :oPMike07/08/00 23:13
Great up 'til the TABLE functionsMike07/08/00 04:14
What is "this"?Jason Schock07/04/00 17:04
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.