My new class

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

My new class

Post 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).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Your much better off simply passing an array rather than a bunch of unorganized variables.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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'];
    }
}
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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. :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Maugrim... that's the most helpful post I've gotten so far. Thank you.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
Last edited by Luke on Thu Nov 10, 2005 12:54 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
Post Reply