Php5 DB results object - best implemetation

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
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Php5 DB results object - best implemetation

Post by Stryks »

Hey all,

I posted this as a part of a larger set a ways back for feedback but got nothing, so I thought I'd go through and focus on each component.

This object is a part of a group of classes I'm working on to give a unified interface to different databases. MySQL and SQLite being the primaries. I have a base class which creates this class and hands it to a db interaction class to be populated, and then passes it back to the user.


Code: Select all

<?php

class dataset implements Iterator {

	private $type;
	
	public $error = false;
	public $debug;
	
	protected $data = NULL;
	protected $detail = array('affected'=>0, 'returned'=>0);

	####################################
   	#### MAGIC METHODS
   	#################################### 	
	public function __construct($sql_type){
		$this->sql_type = $sql_type;
	}
	
    public function __toString() {
    	if($this->detail['affected'] > 0) return $this->detail['affected']; 
    	elseif($this->detail['returned'] > 0) return $this->detail['returned'];
    	else return 0;
	}
	
    public function __set($name, $value) {
    	if(isset($this->detail[$name])) $this->detail[$name] = $value;
    	if($name == 'data') $this->data = $value;
	}

   	####################################
   	#### PUBLIC METHODS
   	####################################	
   		
	public function setError($debug_text) {
		$this->error = true;
		$this->debug = $debug_text;	
	}
	
	public function num_rows() {
		return $this->detail['returned'];
	}
	
	public function affected_rows() {
		return $this->detail['affected'];
	}
	

   	####################################
   	#### ITERATOR METHODS
   	####################################
    
    public function rewind() {
        reset($this->data);
    }

    public function current() {
        $var = current($this->data);
        return $var;
    }

    public function key() {
        $var = key($this->data);
        return $var;
    }

    public function next() {
        $var = next($this->data);
        return $var;
    }

    public function valid() {
        $var = $this->current() !== false;
        return $var;
    }
      	  
}
?>
So my questions are, well, is this a good implementation?

I've opted to have the implementor class pass all results to the dataset as an array. This is mainly so that I can close the query right away instead of waiting for it to reach the end of the results, and also the results can be iterated multiple times, although I rarely do this myself. Any problems with this method?

I originally wrote a function to pass the results to the dataset object, but I was tinkering with things and I rigged the __set() function to allow it to be passed that way. I have no real idea of the implications of this, apart from it not being overly intuitive as to how to populate the dataset. I think my idea was to keep the object as clean of functions as possible. Good or bad approach?

Also, I had originally mapped out this class as two separate ones ... row returning results, and non row returning results. So, one for selects and one for say, creates or inserts. I made it hybrid by once again using the __set() function to allow setting of whatever is the most appropriate. I was hesitant to put the calls in to retrieve this extra data before being called, but when I'm not 'hasty coding' I always call them to verify success anyhow, so I included them. Is there any argument for separating them into two classes, or does this method get the job done cleanly enough?

I'm really just jumping into the deep end with this, so I'm really flying blind as to if I'm doing it the best way. Anything else you would suggest?

Oh, and I just noticed that I don't use sql_type anywhere. I think that was my first attempt to make this contain result specific info. The implementor does that now. Oh, and I haven't left a way to get the last insert ID.

Thanks for any feedback
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

There have been a number of discussions about result object in these forums. You might want to search and read them.

Since you have no specific questions, perhaps you would like to have this moved to Code Critique or Theory?
(#10850)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Thanks for the reply arborint.

I have actually read many threads about this, as well as fair chunk of reading about OOP in the php website. My understanding of OOP is pretty limited, but knowing the way my mind works, I know that the best way for me to learn is to put something functional together myself.

I've read so many posts about this now, that if I took them literally, I'd have to say "well, DB functions don't need a wrapper class ... they're abstract enough". I don't feel that way myself though. Not yet anyways. Maybe I will when I've progressed a bit more.

Now, as for the looking at other examples, as I said, I have read alot on it in here, however without just changing my class into one of those, I cant really get a feel for the benefit, and doing that would deny me the benefit of really understanding the concepts at work here. Also, none of the ones that resemble mine seem to carry much in the way of discussion of why things were done one way and not another, which is more along the lines of what I'm after.

If this fits better in another forum, then I'm happy to have this thread moved, however I do feel that I have asked specific questions, albeit questions that don't stem from warnings or errors.

I've toyed with classes for a while now and I really want to sink my teeth in. All I ask is that you guys are a little patient with me and the open ended questions while I come to grips with it.

Cheers
Post Reply