Trying so hard to work with classes...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Trying so hard to work with classes...

Post by Jim »

I'm trying to connect to a mysql db using classes. The following is some of the code I want to use. No, that's not my real DB info :)

This doesn't work:

Code: Select all

<?
class MySQL {

var $user;
var $pass;
var $host;
var $dbname;

	function dbconnect() {

	$this->user = 'User';
	$this->pass = "tst";
	$this->host = "localhost";
	$this->dbname = "thingy";

	$connect = mysql_connect( $host , $user , $pass );
	$sql = mysql_select_db( $dbname , $connect );

	if(!$connect) {
	
	echo mysql_error();
}

}

?>
Please help me understand why. I'm looking through the manual and at a few tutorials and I don't understand it.

Thanks for your help!
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

you understand here:

Code: Select all

$this->user = 'User';
   $this->pass = "tst";
   $this->host = "localhost";
   $this->dbname = "thingy";
but not here :

Code: Select all

$connect = mysql_connect( $host , $user , $pass );
   $sql = mysql_select_db( $dbname , $connect );
how about :

Code: Select all

$connect = mysql_connect( $this->host , $this->user , $this->pass );
   $sql = mysql_select_db( $this->dbname , $connect );
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

It worked! Thanks a lot, man. You've opened a whole new understanding in to the world of classes for me.

Muchos gracias!
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

lol - enjoy :D
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

Just don't fall into the trap of trying to use classes with everything from now on. ;)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

DeGauss wrote:Just don't fall into the trap of trying to use classes with everything from now on. ;)
Rgr. All you really need is:

Code: Select all

<?php
   $connect = mysql_connect( $host , $user , $pass ); 
   $sql = mysql_select_db( $dbname , $connect ); 

   if(!$connect) { 
    
   echo mysql_error(); 
?>
You don't need the overhead of a class - or even a function definition.

Of course, you maybe plan to add in other functionality to the class, or maybe you just wanted a simple example to start off writing classes.

I think, as a general rule, classes are a good way to go if you have lots of functions in a script which share several variables and it's hard to pass them all sequentially but, for efficient code, don't use classes if you can do it with functions and don't use functions if you can just write some loose code.
Last edited by McGruff on Thu Aug 11, 2005 12:04 pm, edited 1 time in total.
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

this topic seems negative about classes

obv you shouldnt use them all the time but they def have theyre place

the benefits of oop are huge if used correctly and where nessacary

pear uses only classes, which is perfect - any other way would of been silly.
spammich
Forum Newbie
Posts: 11
Joined: Sun Mar 23, 2003 12:40 am

Post by spammich »

How 'bout using PEAR:DB or DB_DatabaseObject?

The nice thing about database abstraction (besides being able to switch databases which, I admit, doesn't really happen that often) is having the classes automatically addslashes() and stripslashes(). Doing that stuff is very important for security, but is a real pain in the ass to do everytime you query your database.

DB_DatabaseObject looks very cool (I haven't used it in a project yet). It has a feature that will let you update or insert a whole table from an assoc array, for example a _POST variable. It also has validation and debug utils.

Check out this tutorial:
http://pear.php.net/manual/en/packages. ... object.php
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Not exactly negative but since a class is a little slower and creating an object grabs a chunk of memory, it's good to ask if it's justified.

Using a whole database abstraction layer solely to avoid having to add/strip slashes may be convenient but it's not efficient code.

I think a lot of classes could be criticised in the same way: easier for the programmer but not the most optimal code for the client. Not the way I like to work.

And maybe not that good for the prgrammer either if a site suddenly becomes very popular and you have to re-write everything more efficiently to reduce server load. Better if scalability is there from the start.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Funny stuff...

All this time I thought learning classes would be one of the most important steps in my PHP learning career.

Now it doesn't seem all that important.

Talk about disillusionment :(

:D
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Just to make sure I'm not giving out the wrong impression there are genuine uses for classes - but always ask yourself if there's a more efficient way to do the same thing (and same for all you're code really).
Post Reply