Re: [PHP] Classes vs. functions?

2003-06-03 Thread Steven Walker
Can someone please explain to me when is a class useful over a set of 
functions?
Almost always. Object oriented programming offers many many advantages 
(that I cannot get into depth on here). There are a few exceptions 
where you would not use classes, such as utility functions like max() 
and min() that would be used generically.

The most basic purpose of a class is to define your own 'type' of 
object. When you create a class you are encapsulating an 'idea' into a 
set of attributes and behaviors specific to that type.

Designing classes not only offers a great way of organizing code but 
inevitably forces you to think thoroughly about the logical separation 
of code and to refine more and more towards an idealized type with well 
defined and sensible behavior. Conversely non-object oriented 
programming tends to lack in clarity and intended usage, making it 
difficult and confusing to use, getting exponentially worse for complex 
problems.

Classes have methods (functions) that are designed to work specifically 
on members of that class or perform operations directly related to it. 
Plain functions, however, have a tendency to be vague and complicated 
by argument lists. Furthermore they are easier to be misused and can be 
prone to latent errors.

In the end, it's like driving a car: the engine is neatly hidden under 
the hood and you don't care or want to know about the internal workings 
of it. You just want to get in, turn the key and drive. Classes are no 
different... they provide a simplified user-friendly interface to 
complex things. It's luxury in programming :)

I would suggest getting some books to learn further, and just start 
using classes.

Steven J. Walker
Walker Effects
www.walkereffects.com
[EMAIL PROTECTED]
On Monday, June 2, 2003, at 08:31 PM, Vijay Avarachen wrote:

I have been mostly using function in my php code, and lately I have 
been
curious about classes.  I have a vague idea of classes from  my C++ 
class
that I took a few years back.  Can someone please explain to me when 
is a
class useful over a set of functions?  I have seen very few code with
classes, so pretty much always stayed away from it.

Thanks,
Vijay Avarachen


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Classes vs. functions?

2003-06-03 Thread olinux
Here is an excellent article describing
classes/objects - what they are and are not. 

http://phpmag.net/itr/online_artikel/psecom,id,284,nodeid,114.html

olinux

  Can someone please explain to me when is a class
 useful over a set of 
  functions?

__
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Classes vs. Functions

2002-07-17 Thread Christopher J. Crane

Thank you for your 2 cents I am just learning and appreciate your comments.
- Original Message -
From: Michael Hall [EMAIL PROTECTED]
To: Chris Crane [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Sent: Tuesday, July 16, 2002 11:13 PM
Subject: Re: [PHP] Classes vs. Functions



 There is no simple answer here. I have started using classes where I find
 I am writing a lot of related functions that share similar
 parameters. Database connection and queries are a good
 example. Authentication is another.

 I have another class that builds forms, because I just hate the tedium of
 coding HTML forms by hand. It is really just a collection of functions,
 though, and could work fine as such.

 I'm still learning/exploring ... I am always guided by the principle that
 whatever makes less work for me (but achieves the same result) is probably
 a good thing.

 IMHO classes are best for more universal code that really can be used in
 many different places. My functions tend to be more application specific.

 My 2 cents

 Michael

 On Tue, 16 Jul 2002, Chris Crane wrote:

  Could someone please explain the difference between classes and
functions
  and how to use a class. I write alot of PHP, but I never understood this
at
  all. I use an include statement in many of my pages and include a file
with
  a bunch of functions. For instance, I might have a function called
stock();
  In the page I am using I include the file that has this function and I
call
  it like this:
 
  stock($Sym);
 
  I am wondering if I am doing it the wrong way. So I need to better
  understand classes. What is one, and why would you use it?
 
  Thanks.
 
 
 
 

 --
 
 n   i   n   t   i  .   c   o   m
 php-python-perl-mysql-postgresql
 
 Michael Hall [EMAIL PROTECTED]
 







-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Jay Blanchard

[snip]
Could someone please explain the difference between classes and functions
and how to use a class. I write alot of PHP, but I never understood this at
all. I use an include statement in many of my pages and include a file with
a bunch of functions. For instance, I might have a function called stock();
In the page I am using I include the file that has this function and I call
it like this:

stock($Sym);

I am wondering if I am doing it the wrong way. So I need to better
understand classes. What is one, and why would you use it?
[/snip]

A class is the representation of an object, such as a person, place, or
thing.
A function is a group of commands that can be called for a specific purpose.

function addNumbers()

A function can be performed on an object, but an object (the logical
extension of class) cannot be performed on a function. Does that help?

Jay

Cleverly disguised as a responsible adult

*
* Want to meet other PHP developers *
* in your area? Check out:  *
* http://php.meetup.com/*
* No developer is an island ... *
*



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Martin Clifford

To add to the below, object classes, can have their own set of functions, called 
methods.  These methods are specific to the function, and ALL functions within a class 
treat any properties (variables) within the class as global.

So if you have a class Car with two properties (Make and Model), then those two 
properties can be readily accessible to any method within the class.  At least I think 
that's how it works.  I'm not all that knowledgeable on the subject :o)

Martin Clifford
Homepage: http://www.completesource.net
Developer's Forums: http://www.completesource.net/forums/


 Jay Blanchard [EMAIL PROTECTED] 07/16/02 10:51AM 
[snip]
Could someone please explain the difference between classes and functions
and how to use a class. I write alot of PHP, but I never understood this at
all. I use an include statement in many of my pages and include a file with
a bunch of functions. For instance, I might have a function called stock();
In the page I am using I include the file that has this function and I call
it like this:

stock($Sym);

I am wondering if I am doing it the wrong way. So I need to better
understand classes. What is one, and why would you use it?
[/snip]

A class is the representation of an object, such as a person, place, or
thing.
A function is a group of commands that can be called for a specific purpose.

function addNumbers()

A function can be performed on an object, but an object (the logical
extension of class) cannot be performed on a function. Does that help?

Jay

Cleverly disguised as a responsible adult

*
* Want to meet other PHP developers *
* in your area? Check out:  *
* http://php.meetup.com/*
* No developer is an island ... *
*



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Chris Crane

It helps a little bit, thank you. Could you provide some code as to what a
Class looks like. I am just trying to understand it better and if I see it,
it might help.
Jay Blanchard [EMAIL PROTECTED] wrote in message
000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt...
 [snip]
 Could someone please explain the difference between classes and functions
 and how to use a class. I write alot of PHP, but I never understood this
at
 all. I use an include statement in many of my pages and include a file
with
 a bunch of functions. For instance, I might have a function called
stock();
 In the page I am using I include the file that has this function and I
call
 it like this:

 stock($Sym);

 I am wondering if I am doing it the wrong way. So I need to better
 understand classes. What is one, and why would you use it?
 [/snip]

 A class is the representation of an object, such as a person, place, or
 thing.
 A function is a group of commands that can be called for a specific
purpose.

 function addNumbers()

 A function can be performed on an object, but an object (the logical
 extension of class) cannot be performed on a function. Does that help?

 Jay

 Cleverly disguised as a responsible adult

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Jay Blanchard

[snip]
example?
[/snip]

http://www.devshed.com/Server_Side/PHP/FormValidatorClass/page1.html

Good tutorial

Jay

*
* Want to meet other PHP developers *
* in your area? Check out:  *
* http://php.meetup.com/*
* No developer is an island ... *
*



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Martin Clifford

Here is a sample code (don't take this as gospel... Jay knows more about OOP than I 
do, I'm sure!):

class Car {
var $make;
var $model;

function setMake($x) {
$this-make = $x;
}
function setModel($y) {
$this-model = $y;
}
}

$make and $model are the properties of the Car class, and setMake() and setModel() are 
the methods of the Car class.  You don't actually get an object until make a *new* 
one.  Like this:

$passat = new Car;
$passat-setMake(Volkswagen);
$passat-setModel(Passat);

This assigns the new object, passat, the properties relating to it's make and model, 
using the methods setMake and setModel.  I'm sure this is right, but I'm sure I'm 
probably wrong (hehe, confused?).  I'm also sure someone will correct me if I am! :o)

Martin Clifford
Homepage: http://www.completesource.net
Developer's Forums: http://www.completesource.net/forums/


 Chris Crane [EMAIL PROTECTED] 07/16/02 11:06AM 
It helps a little bit, thank you. Could you provide some code as to what a
Class looks like. I am just trying to understand it better and if I see it,
it might help.
Jay Blanchard [EMAIL PROTECTED] wrote in message
000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt...
 [snip]
 Could someone please explain the difference between classes and functions
 and how to use a class. I write alot of PHP, but I never understood this
at
 all. I use an include statement in many of my pages and include a file
with
 a bunch of functions. For instance, I might have a function called
stock();
 In the page I am using I include the file that has this function and I
call
 it like this:

 stock($Sym);

 I am wondering if I am doing it the wrong way. So I need to better
 understand classes. What is one, and why would you use it?
 [/snip]

 A class is the representation of an object, such as a person, place, or
 thing.
 A function is a group of commands that can be called for a specific
purpose.

 function addNumbers()

 A function can be performed on an object, but an object (the logical
 extension of class) cannot be performed on a function. Does that help?

 Jay

 Cleverly disguised as a responsible adult

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Chris Crane

This helps quite a bit Thank you.
I am just wondering if I should make classes instead of functions? What
would be the benefit of that? Do you know?

Martin Clifford [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
Here is a sample code (don't take this as gospel... Jay knows more about OOP
than I do, I'm sure!):

class Car {
var $make;
var $model;

function setMake($x) {
$this-make = $x;
}
function setModel($y) {
$this-model = $y;
}
}

$make and $model are the properties of the Car class, and setMake() and
setModel() are the methods of the Car class.  You don't actually get an
object until make a *new* one.  Like this:

$passat = new Car;
$passat-setMake(Volkswagen);
$passat-setModel(Passat);

This assigns the new object, passat, the properties relating to it's make
and model, using the methods setMake and setModel.  I'm sure this is right,
but I'm sure I'm probably wrong (hehe, confused?).  I'm also sure someone
will correct me if I am! :o)

Martin Clifford
Homepage: http://www.completesource.net
Developer's Forums: http://www.completesource.net/forums/


 Chris Crane [EMAIL PROTECTED] 07/16/02 11:06AM 
It helps a little bit, thank you. Could you provide some code as to what a
Class looks like. I am just trying to understand it better and if I see it,
it might help.
Jay Blanchard [EMAIL PROTECTED] wrote in message
000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt...
 [snip]
 Could someone please explain the difference between classes and functions
 and how to use a class. I write alot of PHP, but I never understood this
at
 all. I use an include statement in many of my pages and include a file
with
 a bunch of functions. For instance, I might have a function called
stock();
 In the page I am using I include the file that has this function and I
call
 it like this:

 stock($Sym);

 I am wondering if I am doing it the wrong way. So I need to better
 understand classes. What is one, and why would you use it?
 [/snip]

 A class is the representation of an object, such as a person, place, or
 thing.
 A function is a group of commands that can be called for a specific
purpose.

 function addNumbers()

 A function can be performed on an object, but an object (the logical
 extension of class) cannot be performed on a function. Does that help?

 Jay

 Cleverly disguised as a responsible adult

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Steve Bradwell
  $this-numberrows; $x++) {
$this-result[$x] = mysql_fetch_row($this-qry);
}
} else {
//echo([Error:] Retrieving data);
return false;
}
return true;
}
}

function insertquery() {

global $TRUE, $FALSE;

if ($this-dbconnection == $FALSE) {
$this-opendbconnection();
}

   $this-qry = mysql_query($this-sql);
   if (!$this-qry) {
   return false;
   } else {
   return true;
   }
}

function deletequery() {

global $TRUE, $FALSE;

if ($this-dbconnection == $FALSE) {
$this-opendbconnection();
}

   $this-qry = mysql_query($this-sql);
   if (!$this-qry) {
   return false;
   } else {
   return true;
   }
}

function updatequery() {

global $TRUE, $FALSE;

if ($this-dbconnection == $FALSE) {
$this-opendbconnection();
}

   $this-qry = mysql_query($this-sql);
   if (!$this-qry) {
   return false;
   } else {
   return true;
   }
}

}

?

And now in any .php file you include this .obj file and use it as follows.

$db1 = new mysqldb(); // create a new instance of the mysql object.
  // You can create as many as you want and
the great thing is
  // that each instances vars will contain
there own data.

$sql = Insert into ...;
$db1-setTransactionSwitch(true);
$db1-begin();  //this will begin a transaction (InnoDb or BDB tables
are required for this)
 $db1-setsql($sql);  //set the objects $sql variable.
  if (!$db1-insertquery())  //now call the method that does
all the work so 
$db1-setTransactionSwitch(false); //you don't have to recode it.
If it fails roolback. 
 if($db1-getTransactionSwitch()==false);{
$db1-rollback();
 }else{
$db1-commit();
 }

 Classes allow you to have multple instances of an object containing
seperate info, makes it easier to keep track of.
Hope this helps,
Steve.

-Original Message-
From: Chris Crane [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 16, 2002 11:07 AM
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Classes vs. Functions


It helps a little bit, thank you. Could you provide some code as to what a
Class looks like. I am just trying to understand it better and if I see it,
it might help.
Jay Blanchard [EMAIL PROTECTED] wrote in message
000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt...
 [snip]
 Could someone please explain the difference between classes and functions
 and how to use a class. I write alot of PHP, but I never understood this
at
 all. I use an include statement in many of my pages and include a file
with
 a bunch of functions. For instance, I might have a function called
stock();
 In the page I am using I include the file that has this function and I
call
 it like this:

 stock($Sym);

 I am wondering if I am doing it the wrong way. So I need to better
 understand classes. What is one, and why would you use it?
 [/snip]

 A class is the representation of an object, such as a person, place, or
 thing.
 A function is a group of commands that can be called for a specific
purpose.

 function addNumbers()

 A function can be performed on an object, but an object (the logical
 extension of class) cannot be performed on a function. Does that help?

 Jay

 Cleverly disguised as a responsible adult

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Jay Blanchard

[snip]
This helps quite a bit Thank you.
I am just wondering if I should make classes instead of functions? What
would be the benefit of that? Do you know?
[/snip]

You should keep classes and functions seperate in your thought process, they
are not the same and not meant to do or be the same thing. Here is a good
thought process to apply to classes...

1. Is this an object?

If the answer is no, you do not need a class.

2. Do I need to do something (have an action or set of actions performed),
perhaps over and over again?

If the answer is yes, this is a function.

A method is a function that applies to a specific class of objects. If you
created a class and methods for each item you code would be bloated, slow,
and hard to maintain.

Do not think of functions and classes in the same light, you will make
yourself more confused. One is definitely not the other.

Jay

*
* Want to meet other PHP developers *
* in your area? Check out:  *
* http://php.meetup.com/*
* No developer is an island ... *
*



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Martin Clifford

I'm still trying to figure that out, but the fog is clearing slowly but steadily :o)

From what I've heard on this and other lists, it's all a matter of preference.  
Obviously those that come from an object-oriented environment (Java, etc), will lean 
toward this method, while others stay with the procedural side of things (using 
functions).  It's all a matter of preference, and just yet I haven't decided which is 
more useful :o)

Good luck!

Martin Clifford
Homepage: http://www.completesource.net
Developer's Forums: http://www.completesource.net/forums/


 Chris Crane [EMAIL PROTECTED] 07/16/02 11:19AM 
This helps quite a bit Thank you.
I am just wondering if I should make classes instead of functions? What
would be the benefit of that? Do you know?

Martin Clifford [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
Here is a sample code (don't take this as gospel... Jay knows more about OOP
than I do, I'm sure!):

class Car {
var $make;
var $model;

function setMake($x) {
$this-make = $x;
}
function setModel($y) {
$this-model = $y;
}
}

$make and $model are the properties of the Car class, and setMake() and
setModel() are the methods of the Car class.  You don't actually get an
object until make a *new* one.  Like this:

$passat = new Car;
$passat-setMake(Volkswagen);
$passat-setModel(Passat);

This assigns the new object, passat, the properties relating to it's make
and model, using the methods setMake and setModel.  I'm sure this is right,
but I'm sure I'm probably wrong (hehe, confused?).  I'm also sure someone
will correct me if I am! :o)

Martin Clifford
Homepage: http://www.completesource.net 
Developer's Forums: http://www.completesource.net/forums/ 


 Chris Crane [EMAIL PROTECTED] 07/16/02 11:06AM 
It helps a little bit, thank you. Could you provide some code as to what a
Class looks like. I am just trying to understand it better and if I see it,
it might help.
Jay Blanchard [EMAIL PROTECTED] wrote in message
000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt...
 [snip]
 Could someone please explain the difference between classes and functions
 and how to use a class. I write alot of PHP, but I never understood this
at
 all. I use an include statement in many of my pages and include a file
with
 a bunch of functions. For instance, I might have a function called
stock();
 In the page I am using I include the file that has this function and I
call
 it like this:

 stock($Sym);

 I am wondering if I am doing it the wrong way. So I need to better
 understand classes. What is one, and why would you use it?
 [/snip]

 A class is the representation of an object, such as a person, place, or
 thing.
 A function is a group of commands that can be called for a specific
purpose.

 function addNumbers()

 A function can be performed on an object, but an object (the logical
 extension of class) cannot be performed on a function. Does that help?

 Jay

 Cleverly disguised as a responsible adult

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php 





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Chris Crane

thanks Jay.
Jay Blanchard [EMAIL PROTECTED] wrote in message
000e01c22cdd$0d0c7530$8102a8c0@niigziuo4ohhdt">news:000e01c22cdd$0d0c7530$8102a8c0@niigziuo4ohhdt...
 [snip]
 This helps quite a bit Thank you.
 I am just wondering if I should make classes instead of functions? What
 would be the benefit of that? Do you know?
 [/snip]

 You should keep classes and functions seperate in your thought process,
they
 are not the same and not meant to do or be the same thing. Here is a good
 thought process to apply to classes...

 1. Is this an object?

 If the answer is no, you do not need a class.

 2. Do I need to do something (have an action or set of actions performed),
 perhaps over and over again?

 If the answer is yes, this is a function.

 A method is a function that applies to a specific class of objects. If you
 created a class and methods for each item you code would be bloated, slow,
 and hard to maintain.

 Do not think of functions and classes in the same light, you will make
 yourself more confused. One is definitely not the other.

 Jay

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Chris Crane

Is there an advantage to Classes vs. Functions?
Jay Blanchard [EMAIL PROTECTED] wrote in message
000c01c22cdb$11485c10$8102a8c0@niigziuo4ohhdt">news:000c01c22cdb$11485c10$8102a8c0@niigziuo4ohhdt...
 [snip]
 example?
 [/snip]

 http://www.devshed.com/Server_Side/PHP/FormValidatorClass/page1.html

 Good tutorial

 Jay

 *
 * Want to meet other PHP developers *
 * in your area? Check out:  *
 * http://php.meetup.com/*
 * No developer is an island ... *
 *





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Chris Crane
urn true;
 }
 }

 function selectquery() {

 global $TRUE, $FALSE;

 if ($this-dbconnection == $FALSE) {
 $this-opendbconnection();
 }
 $this-qry = mysql_query($this-sql);
 if (!$this-qry) {
 return false;
 } else {
 $this-numberrows = mysql_num_rows($this-qry);
 if ($this-numberrows  0) {
 for($x = 0; $x  $this-numberrows; $x++) {
 $this-result[$x] = mysql_fetch_row($this-qry);
 }
 } else {
 //echo([Error:] Retrieving data);
 return false;
 }
 return true;
 }
 }

 function insertquery() {

 global $TRUE, $FALSE;

 if ($this-dbconnection == $FALSE) {
 $this-opendbconnection();
 }

$this-qry = mysql_query($this-sql);
if (!$this-qry) {
return false;
} else {
return true;
}
 }

 function deletequery() {

 global $TRUE, $FALSE;

 if ($this-dbconnection == $FALSE) {
 $this-opendbconnection();
 }

$this-qry = mysql_query($this-sql);
if (!$this-qry) {
return false;
} else {
return true;
}
 }

 function updatequery() {

 global $TRUE, $FALSE;

 if ($this-dbconnection == $FALSE) {
 $this-opendbconnection();
 }

$this-qry = mysql_query($this-sql);
if (!$this-qry) {
return false;
} else {
return true;
}
 }

 }

 ?

 And now in any .php file you include this .obj file and use it as follows.

 $db1 = new mysqldb(); // create a new instance of the mysql object.
   // You can create as many as you want and
 the great thing is
   // that each instances vars will contain
 there own data.

 $sql = Insert into ...;
 $db1-setTransactionSwitch(true);
 $db1-begin();  //this will begin a transaction (InnoDb or BDB tables
 are required for this)
  $db1-setsql($sql);  //set the objects $sql variable.
   if (!$db1-insertquery())  //now call the method that
does
 all the work so
 $db1-setTransactionSwitch(false); //you don't have to recode
it.
 If it fails roolback.
  if($db1-getTransactionSwitch()==false);{
 $db1-rollback();
  }else{
 $db1-commit();
  }

  Classes allow you to have multple instances of an object containing
 seperate info, makes it easier to keep track of.
 Hope this helps,
 Steve.

 -Original Message-
 From: Chris Crane [mailto:[EMAIL PROTECTED]]
 Sent: Tuesday, July 16, 2002 11:07 AM
 To: [EMAIL PROTECTED]
 Subject: Re: [PHP] Classes vs. Functions


 It helps a little bit, thank you. Could you provide some code as to what a
 Class looks like. I am just trying to understand it better and if I see
it,
 it might help.
 Jay Blanchard [EMAIL PROTECTED] wrote in message
 000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt">news:000401c22cd8$54ce9ce0$8102a8c0@niigziuo4ohhdt...
  [snip]
  Could someone please explain the difference between classes and
functions
  and how to use a class. I write alot of PHP, but I never understood this
 at
  all. I use an include statement in many of my pages and include a file
 with
  a bunch of functions. For instance, I might have a function called
 stock();
  In the page I am using I include the file that has this function and I
 call
  it like this:
 
  stock($Sym);
 
  I am wondering if I am doing it the wrong way. So I need to better
  understand classes. What is one, and why would you use it?
  [/snip]
 
  A class is the representation of an object, such as a person, place, or
  thing.
  A function is a group of commands that can be called for a specific
 purpose.
 
  function addNumbers()
 
  A function can be performed on an object, but an object (the logical
  extension of class) cannot be performed on a function. Does that help?
 
  Jay
 
  Cleverly disguised as a responsible adult
 
  *
  * Want to meet other PHP developers *
  * in your area? Check out:  *
  * http://php.meetup.com/*
  * No developer is an island ... *
  *
 
 



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Steve Bradwell

Yes any vars used in a class have to be defined ahead. Then just write quick
set and get mothods to change them.

And the important thing to remember is $db1-Host; retrieved with
$db1-getHost(); for example contains seperate data then $db2-$Host;

Good luck.  
 

-Original Message-
From: Chris Crane [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 16, 2002 11:40 AM
To: [EMAIL PROTECTED]
Subject: Re: [PHP] Classes vs. Functions


Wow...this is pretty cool.
Do you HAVE to declareall your varibles ahead of time?
Steve Bradwell [EMAIL PROTECTED] wrote in message
57A1618E7109D311A97D0008C7EBB3A1010C8258@KITCHENER">news:57A1618E7109D311A97D0008C7EBB3A1010C8258@KITCHENER...
 Classes are great for code reusability, I heavily use a MySQL object or
 class to make all my conections to a mysql database now and I have
included
 methods for Transactions.

 Its a long one but its the class I use the most and is a great example of
 what they are good for.


 ?
 class mysqldb {  //so here define the name of the class.

 //set up the object, these are the variables that are accessible by
each
 instance you create
 // of an object.
 var $host;
 var $db;
 var $dbuser;
 var $dbpassword;
 var $sql;
 var $numberrows;
 var $dbopenstatus;
 var $dbconnection;
 var $qry;
 var $result;
 var $TransactionSwitch;
 /*
 Use these functions to get and set the values of this object's
 variables. This is good OO practice, as it means that datatype
 checking can be completed and errors raised accordingly.

 */

 // Property Get  Set these methods are used set class vars and to
 retrieve them.

 function gethost() {
 return $this-dbhost;
 }

 function sethost($req_host) {
 $this-dbhost = $req_host;

 }

 function getdb() {
 return $this-db;
 }

 function setdb($req_db) {
 $this-db = $req_db;
 }

 function getdbuser() {
 return $this-dbuser;
 }

 function setdbuser($req_user) {
 $this-dbuser = $req_user;
 }

 function getdbpassword() {
 return $this-dbpassword;
 }

 function setdbpassword($req_password) {
 $this-dbpassword = $req_password;
 }

 function getsql() {
 return $this-sql;
 }

 function setsql($req_sql) {
 $this-sql = $req_sql;
 }

 function getnumberrows() {
 return $this-numberrows;
 }

 function setnumberrows($req_numberresults) {
 $this-numberesults = $req_numberresults;
 }

 function setdbconnection($req_dbconnection) {
 $this-dbconnection = $req_connection;
 }

 function getdbconnection() {
 return $this-dbconnection;
 }

 function setTransactionSwitch($switch) {
 $this-TransactionSwitch = $switch;
 }

 function getTransactionSwitch() {
 return $this-TransactionSwitch;
 }

 /*
 This is the constructor for the object. In this case I have set
 the initial values of a number of the object properties to those
 values declared in the global constants.inc. By doing this, I
 only need to change the values of these properties for specific
 operations, which we will not need to do throughout this example

 */
 function mysqldb() {

 global $HOST, $DB, $WEBUSER, $WEBPASSWORD;
 global $TRUE, $FALSE;

 $this-sethost($HOST);
 $this-setdb($DB);
 $this-setdbuser($WEBUSER);
 $this-setdbpassword($WEBPASSWORD);
 $this-setdbconnection($FALSE);

 }

 /*
 These are the methods for the object. They provide for opening a
 connection to the database, closing a connection and executing a
 SELECT query. Of course, these can be expanded upon to allow for
 INSERT's, UPDATE's and DELETE's etc...
 */
 function opendbconnection() {

 global $TRUE, $FALSE;

 $this-dbconnection = mysql_connect($this-dbhost,
 $this-dbuser, $this-dbpassword);
 if ($this-dbconnection == $TRUE) {
 $this-db = mysql_select_db($this-db);
 $this-setdbconnection($TRUE);
 } else {
 $this-setdbconnection($FALSE);
 return false;
 }
 return true;
 }

 function closedbconnection() {

 if ($this-dbconnection = $TRUE) {
 mysql_close($this-dbconnection);
 }

 }

 function begin( ) {
 if ($this-dbconnection == $FALSE) {
 $this-opendbconnection();
 }
 $this-setsql(BEGIN);
 $this-qry = mysql_query($this-sql);
 if (!$this-qry) {
 return false;
 } else {
 return true;
 }
 }

 function rollback( ) {
 if ($this-dbconnection == $FALSE) {
 $this-opendbconnection();
 }
 $this-setsql(ROLLBACK);
 $this-qry = mysql_query($

Re: [PHP] Classes vs. Functions

2002-07-16 Thread Alberto Serra

ðÒÉ×ÅÔ!

Martin Clifford wrote:
 Could someone please explain the difference between classes and functions
 and how to use a class. 

Well, that's a 1 billion $$ question. I don't think one can fully grasp 
that difference by reading an email. I strongly suggest you to buy 
yourself a book about OOP and have a go at it. That is, if you really 
care about knowing.

Whether you should like OOP or not is a religious matter, so I will not 
enter the field. OOP has its pluses and its minuses. It's a technique, 
not an ultimate truth, although it is often presented as such. And as 
any technique, it can do wonders and it can do plain bull**t when not 
properly used.

Nowadays 100% of my work is OOP based, but I worked some 15 years on 
functions and I cannot blame those who keep working that way. There are 
reasons for doing it and reason for not to do it. Get yourself a good 
clear book, then make a decision.

ÐÏËÁ
áÌØÂÅÒÔÏ
ëÉÅ×


-_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_-@-_=}{=_--_=}{=_-

LoRd, CaN yOu HeAr Me, LiKe I'm HeArInG yOu?
lOrD i'M sHiNiNg...
YoU kNoW I AlMoSt LoSt My MiNd, BuT nOw I'm HoMe AnD fReE
tHe TeSt, YeS iT iS
ThE tEsT, yEs It Is
tHe TeSt, YeS iT iS
ThE tEsT, yEs It Is...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Martin Towell

 
 Martin Clifford wrote:
  Could someone please explain the difference between classes and
functions
  and how to use a class. 

 Whether you should like OOP or not is a religious matter, so I will not 
 enter the field. OOP has its pluses and its minuses. It's a technique, 
 not an ultimate truth, although it is often presented as such. And as 
 any technique, it can do wonders and it can do plain bull**t when not 
 properly used.

 Nowadays 100% of my work is OOP based, but I worked some 15 years on 
 functions and I cannot blame those who keep working that way. There are 
 reasons for doing it and reason for not to do it. Get yourself a good 
 clear book, then make a decision.

[snip]

I have to agree here. IMO, there are benefits in using classes over
functions, and there's benefits in using functions over classes. Once you
get to know oop a little better, you should be able to determine which is
better is any given situation.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Classes vs. Functions

2002-07-16 Thread Peter J. Schoenster

On 17 Jul 2002 at 12:43, Michael Hall wrote:


 There is no simple answer here. I have started using classes where I
 find I am writing a lot of related functions that share similar
 parameters. Database connection and queries are a good example.
 Authentication is another.

Yeah.

 I have another class that builds forms, because I just hate the tedium
 of coding HTML forms by hand. It is really just a collection of
 functions, though, and could work fine as such.

This is a gray area imho. I'd leave all html to the person who cares 
what it looks like, not what it does. I usually also use a code 
generator to create html and their forms but they are a separate 
layer. I use templates, wish a lot more php people would as well 
although I've seen some weird stuff where in this one bb they store 
templates in the database. That's interesting. 

 I'm still learning/exploring ... I am always guided by the principle
 that whatever makes less work for me (but achieves the same result) is
 probably a good thing.
 
 IMHO classes are best for more universal code that really can be used
 in many different places. My functions tend to be more application
 specific.

Yeah, can't say too much more than that. There is the style of coding 
where one application is completely independent of another. Then you 
begin to realize, gee ... I could just cut and paste this code. And 
then there's always the funny repetion of the exact same code every 
30 lines or so (depending on memory of programmer I guesss). 
Eventually you begin to realize gee ... could I put this stuff in a 
library. A CLASS after all is just a collection of functions with a 
data model. But ... there is modular and then there is OO imho. I'm a 
die hard modular programmer who is trying to think in a more OO way. 
But of course when you just gotta get something done, do it. The 
value in spending a bit more time going the modular/OO route is that 
your application will be easier to evolve and debug.

Peter-- http://www.readbrazil.com/
Answering Your Questions About Brazil


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Classes vs. Functions

2002-07-16 Thread Martin Towell

[snip]
 A CLASS after all is just a collection of functions with a
 data model. But ... there is modular and then there is OO imho.
[snip]

A class is more than just a bunch of functions that have been placed
together. If you want to do that, then you might as well just throw them all
into the same include file and that's it. There's no added benefit in
wrapping a class around them.

Agreed, a class can be used to simulate the C-style struct, or to collect a
bunch of functions, but the real power of classes is when you start to model
the real world objects (ie, methods and properties, not just functions and
variables.)

I'm not saying to go all OO (or all procedule, I use both...), just that
when used correctly, they can be really helpful.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php