Page 1 of 1

Trying so hard to work with classes...

Posted: Mon Apr 07, 2003 5:53 am
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!

Posted: Mon Apr 07, 2003 6:28 am
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 );

Posted: Mon Apr 07, 2003 8:32 am
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!

Posted: Mon Apr 07, 2003 9:02 am
by d1223m
lol - enjoy :D

Posted: Mon Apr 07, 2003 9:08 am
by DeGauss
Just don't fall into the trap of trying to use classes with everything from now on. ;)

Posted: Mon Apr 07, 2003 10:21 am
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.

Posted: Mon Apr 07, 2003 10:29 am
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.

Posted: Mon Apr 07, 2003 11:02 am
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

Posted: Mon Apr 07, 2003 1:38 pm
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.

Posted: Mon Apr 07, 2003 2:01 pm
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

Posted: Mon Apr 07, 2003 2:07 pm
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).