Page 1 of 1

My new class

Posted: Wed Nov 09, 2005 7:02 pm
by Luke
This is the class I was talking about in the previous thread "Classes" if you were helping me in there...
OK... here's what I have so far...

Code: Select all

<?php
include_once("../includes/functions.php");
class pDirect{
	var $id;
	var $name;
	var $description;
	var $address;
	var $city;
	var $state;
	var $zip;
	var $phone;
	var $email;
	var $website;
	var $st_hosted;
	var $categories;
	var $misc;
	
	var $host;
	var $user;
	var $pass;
	var $dbas;
    function pDirect(){
	    $this->host = "host";
	    $this->user = "user";
	    $this->pass = "pass";
	    $this->dbas = "directory";
		if(mysql_connect($this->host, $this->user, $this->pass)){
			if(!mysql_select_db($this->dbas){
				logerror("Could not select database: ".$this->dbas." MySQL Says: ".mysql_error());
			}
		}
		else{
			logerror("Could not connect to database with user: ".$this->user." MySQL Says: ".mysql_error());
		}
    }
    function addDb(){
		$sql = "INSERT INTO ".$this->dbas." VALUES('".$this->id."', '".$this->name."', '".$this->description."', '".$this->address."', '".$this->city."', '".$this->state."', '".$this->zip."', '".$this->phone."', '".$this->email."', '".$this->website."', '".$this->st_hosted."', '".$this->categories."', '".$this->misc."')";
		if(mysql_query($sql)){
			return true;
		}
		logerror("Could not execute query: ".$sql." Mysql Says: ".mysql_query());
		return false;
    }
    function delDb(){
		$sql = "DELETE FROM ".$this->dbas." WHERE `id` = '".$this->id."'";
		if(mysql_query($sql)){
			return true;
		}
		logerror("Could not execute query: ".$sql." Mysql Says: ".mysql_query());
		return false;
    }
}
?>
I don't know how the variables are supposed to be initially set (the vars at the beginning).

Is there a standard way to set variables (since I am setting up a web interface that will accept these values from a form and send them to this class).

Posted: Wed Nov 09, 2005 7:39 pm
by Chris Corbyn
Write methods to set the values of the properties. I'm not exclusively talking about setters anymore neither. If you're processing data some of those properties may change in the course of running some procedure.

It's *possible* that you don't even need all of those as defined properties anyway if you're simply going to set them up and then insert some data and destroy them.

I *personally* don't have an issue with using the superglobals ( $_POST, $_GET, $_SESSION ) inside a class and I guess that those value will be coming from there.

Posted: Wed Nov 09, 2005 8:06 pm
by John Cartwright
Your much better off simply passing an array rather than a bunch of unorganized variables.

Posted: Thu Nov 10, 2005 11:06 am
by Luke
Passing an array like this... ?

Code: Select all

class myClass{
    function myClass($array){
        $this->whatever = $array['whatever'];
        $this->foo = $array['foo'];
        $this->bar = $array['bar'];
    }
}

Posted: Thu Nov 10, 2005 11:20 am
by Maugrim_The_Reaper
Maybe - nothing wrong with using superglobals if they *are* the array you're using explicitly. Just remember to filter them, escape, etc. Security needs to be kept in mind...

DB could be a separate object - i.e. a segregate class for creating a database connection for use in other objects as required. Usually a good practice to make classes as lean and single-task as possible (makes them more re-usable elsewhere in other projects even).

Posted: Thu Nov 10, 2005 11:24 am
by Luke
Damn... I'm really confused. I've been told like 10 things by 10 different people on how to make classes. I don't even know where to start now.

Posted: Thu Nov 10, 2005 11:35 am
by Maugrim_The_Reaper
Fun, isn't it? ;)

It's because we all have out own practices. Welcome to OOP...

Some of the suggestions are broader best practices in OOP. For example - including DB methods into a class, along with the data to be used, makes it difficult to re-use any of your code elsewhere - without the tiresome task of cut'n'paste.

Note - if your class works - it works. But in the interested of a demo; I'd do the same thing using a small collection of classes.

1. A data class (name varies...a lot) basically a wrapper for the array of data you're using, usually the *filtered* data (for security)
2. The Data Access class (all your SQL calls); methods would accept as a parameter the data class above.
3. Business Object; basically what ties the two together.

For example - just to kick off a project, I might use a standard Data Access class that literally constructs the SQL for stock Create/Read/Update/Delete operations. Completely re-usable. Anywhere. DB Connections would be handled by yet another re-useable stock class (it's usually the same process in most applications after all).

This is all just food for thought - when this is used on a large growing application, these same ideas will probably start occuring to you naturally. :)

Posted: Thu Nov 10, 2005 11:56 am
by Luke
Maugrim... that's the most helpful post I've gotten so far. Thank you.

Posted: Thu Nov 10, 2005 12:42 pm
by Luke
OK, so here's what I've got for the class that handles database transactions...

Code: Select all

class DataBase{
	function DataBase($host="localhost", $user="root", $pass="8ateball", $dbas="pdirect_directory"){
		if(mysql_connect($host, $user, $pass)){
			if(!mysql_select_db($dbas)){
				logerror("Could not select database - " . $dbas . "\n MySQL says: " . mysql_error());
			}
		}
		else{
			logerror("Could not to " . $host . " connect with user - " . $user . "\n MySQL says: " . mysql_error());
		}
	}
	function Insert($values, $table){
		$sql = "INSERT INTO " . $table . " VALUES ( ";
		foreach($values as $val){
			$sql .= " '" . mysql_real_escape_string($val) . "',";
		}
		$sql = substr($sql, 0, -1);
		$sql .= ");";
		if(mysql_query($sql)){
			return true;
		}
		logerror("Could not execute query: " . $sql . "\n MySQL says: " . mysql_error());
		return false;
	}
}


$connection = new DataBase();
$array = array('id'          => '',
			   'name'        => "Name",
			   'description' => "description",
			   'address'     => "address",
			   'city'        => "city",
			   'state'       => "state",
			   'zip'         => "zip",
			   'phone'       => "phone",
			   'email'       => "email",
			   'website'     => "website",
			   'st_hosted'   => "st_hosted",
			   'categories'  => "categories");
$connection->Insert($array, 'directory');
What I get from that is this...
INSERT INTO directory VALUES ( '', 'Name', 'description', 'address', 'city', 'state', 'zip', 'phone', 'email', 'website', 'st_hosted', 'categories');
Which is what I want... now how would I want to go about a function that handles the actual data?

Posted: Thu Nov 10, 2005 12:44 pm
by Luke
Maugrim_The_Reaper wrote:1. A data class (name varies...a lot) basically a wrapper for the array of data you're using, usually the *filtered* data (for security)
OK, so this is the part I want to do after I finish the database class... what would this consist of?