Using classes, check this out plz.

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
s3rg1o
Forum Commoner
Posts: 32
Joined: Sun Feb 16, 2003 4:58 pm

Using classes, check this out plz.

Post by s3rg1o »

Hey guys I have been using classes lately and I recently replaced an old script I was using and used classes.

But I don't want to get into bad habbits I was wondering if I could get this community to take a look at the coding to see if maybe I did something wrong or I did it inefficiently. I don't want to get into any bad habbits. The code is fairly simple.

To see the script in action go to http://www.pcgamemods.com/statcheck.php


db.php

Code: Select all

<?

class DB
&#123;
var $host = 'localhost';
var $user = 'root';
var $password = '*****';
var $database = 'pcgamemods';


var $conn = NULL;

var $result = false;

	function DB($host, $user, $password, $database)
	&#123;
	$this->host = $host;
	$this->user = $user;
	$this->password = $password;
	$this->database = $database;

	&#125;




	function open()
	&#123;
	
	$this->conn = mysql_pconnect($this->host, $this->user, $this->password);
	
	if (!$this->conn) &#123;
		return false;
	&#125;
	
	if (@!mysql_select_db($this->database, $this->conn)) &#123;
		return false;
	&#125;
	return true;	
		
	
		
		
	&#125;
	
	
	function close() 
	&#123;
	return (@mysql_close($this->conn));	
		
	&#125;
	
	function error()
	&#123;
		return (mysql_error());
	&#125;
	
	
	function query($sql)
	&#123;
		$this->result = @mysql_query($sql, $this->conn);
		return ($this->result != false);
	&#125;
	
	function affectedRows()
	&#123;
		return (@mysql_affected_rows($this->conn));
	&#125;
	
	function numRows()
	&#123;
		return(@mysql_num_rows($this->result));
	&#125;
	
	function fetchObject()
	&#123;
		return (@mysql_fetch_object($this->result, MYSQL_ASSOC));
	&#125;
	
	function fetchArray()
	&#123;
		return (@mysql_fetch_array($this->result, MYSQL_NUM));
	&#125;
	
	function fetchAssoc()
	&#123; 
		return (@mysql_fetch_assoc($this->result));
	&#125;
	function fetchCount()
	&#123; 
		return (@mysql_fetch_row($this->result));
	&#125;
	function freeResult()
	&#123;
		return (@mysql_free_result($this->result));
	&#125;
	
&#125;
?>
statuscheck.php

Code: Select all

<?

class Status
&#123;

var $dl_num;
var $dl_today;
var $dl_downloading;

	function Status ()
 	&#123;
 		$this->dl_num = $dl_num;
 		$this->dl_today = $dl_today;
 		$this->dl_downloading = $dl_downloading;
	&#125;
 		
	function getdlcount() 
	&#123;
		$db = new DB ('localhost', 'root', '*****',  'pcgamemods');;
		$db->open();
		$db->query("select count(*) from downloads");
		$db->close();
		
		$this->dl_num = $db->fetchCount();
		
		return $this->dl_num&#1111;0];
	&#125;
	function getdlcounttoday($today) 
	&#123;
		$db = new DB ('localhost', 'root', '*****',  'pcgamemods');;
		$db->open();
		$db->query("select * from dlcount where date = '$today'");
		$db->close();
		
		$this->dl_today = $db->fetchAssoc();
		
		return $this->dl_today&#1111;downloads];
	&#125;
	function getdownloading($today) 
	&#123;
		$db = new DB ('localhost', 'root', '*****',  'pcgamemods');
		$db->open();
		$db2 = new DB ('localhost', 'root', '*****',  'pcgamemods');
		$db2->open();
		$db->query("select * from downloads");

			while ($temp = $db->fetchAssoc())
			&#123;
				$db2->query("select * from core where md5id = '$temp&#1111;file]'");	
				$this->dl_downloading = $db2->fetchAssoc();
				
				echo $this->dl_downloading&#1111;'name']. '<BR>';
			&#125;
	&#125;
&#125;
?>
statcheck.php

Code: Select all

<?
require_once("db.php");
require_once("statuscheck.php");

$today = date("Y-n-d");
$status = new Status;

echo 'Now: <B>' .$status->getdlcount(). '</B><BR>';
echo 'Today: <B>' .$status->getdlcounttoday($today). '</B><BR>';


echo ('<BR><B>Files Being Downloaded:</B><BR>');
echo $status->getdownloading($today);



?>
Any suggestions or comments would be appreciated.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'm not an expert on classes by any means, but here's some thoughts.

db.php

Why do you need a constructor if you have already initialised the class vars (or why do you initialise class vars if you need a constructor?).

Also, is there a good reason to put all these functions in a class? The general rule I follow - and I may be wrong - is only to create a class if I have several functions which always work in sequence AND it's awkward to pass vars along the line (maybe because there's lots of them or because the code doesn't operate in a nice, linear manner).

For example, you've got functions for fetching objects, fetching results as non-associative arrays and fetching results as associative arrays. It would be unusual if you needed to use them all in the same script. Rather than writing just the code you need you've defined additional functions which won't be used. The object dies when the script is finished so there's no point in a class unless 90% of its functions are actually going to be used on 90% of the occasions when it's created.

Also, in each case there's an existing php function which does exactly what you've defined as a user-function - why burden the parser with unecessary function definitions?

That's as far as I got - work to do. Hope that's constructive criticism.
s3rg1o
Forum Commoner
Posts: 32
Joined: Sun Feb 16, 2003 4:58 pm

Post by s3rg1o »

oh the constructor..yeh sorry I just added that my bad thanks for the criticism though I was just using this as a benchmark to see if I should use it
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Actually, the constructor is sort of a good idea in a sense. Because instead of editing the actual code in the class you can simply invoke the object with your specific DB information.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Actually, the constructor is sort of a good idea in a sense. Because instead of editing the actual code in the class you can simply invoke the object with your specific DB information.
... but at the expense of an extra function. With a new website, if you're going to edit a line of code which calls the class with the new connection vars, you might as well just edit the class with the same values.

I think programmers often arrange things to make life easier for themselves rather than making lean & fast code for the client. Out of all the classes parsing away on the internet as we speak, I wonder how many are really justified?

It's good to mess around with classes though if you're learning. Stick in there S3g1o.
Last edited by McGruff on Sun Mar 02, 2003 11:28 pm, edited 1 time in total.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

No offense, but another function which simply sets member variables is NOT overkill. A constructor is probably the most important function that a class can have when done properly.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

None taken. Always glad to hear other people's ideas - still learning myself..
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

Using a class for such simple functionality is overkill in my mind, PHP-oo is not efficient (yet), so if you are worried about efficiency don't use oo.. But for an OO script i don't think it looks so bad..

Even if you need multiple instance functionality, OO isnt always the solution, especially not for such simple things as a dynamic website script, in this thread at the bottom I posted a sample of normal function usage with simulated "multiple instances"..

Another thing, does your script connect to more than one database? There is no point connecting more than one time to the same database, you can have muliple query-resources on a single connection..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

at the current state of php I avoid classes where I can (maybe I should test them with 4.3). The only useful functionality they offer is to overload functions and that at a high price.
If you're going to use other databases and you can use the same interface a wrapper class might be useful (if you take care of the extra-overhead mentioned above). Otherwise you gain nothing (in the best case) and have extra code to put extra bugs in ;)
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post by DeGauss »

Ahh... Another PHP user discovers classes.

Now stop using them.

Classes look really cool, and you feel really cool making them and using them, but at the end of the day you should only construct them for complex, repeated tasks that would take too long to reproduce manually.

I started writing a BBS using entirely in classes, because i thought that they were the way to go, and that was my big mistake.

Classes are there to make hard things easy for you to do, not to replace everything you've already learned.

God i hate grammar.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

You guys are right only to a certain degree. Sure, OOP may not always be the way to go when writing a script. But when done correctly, classes can be quite efficient and work really well.

Code according to the way that you feel is the best approach to solving the problem. Benchmark your application if you want. In some of the programs I have written, OOP was faster than the procedural method that I tested with. In others, OOP may have been a hundredth of a second or so slower.

It all depends on the application though. A good design and planning period is what can really make the difference when you begin to write a script.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it was no general call against OOP. The first word I usually write after opening a new project is class :] but not in php
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Heh, right, but these are PHP forums, so obviously I am referring to OOP in PHP.
Post Reply