Click to See Complete Forum and Search --> : [RESOLVED] php overloading __get and __set advice


Dopple
06-17-2008, 08:42 AM
I have been looking at this section of the php manual, http://uk3.php.net/manual/en/language.oop5.overloading.php.
I have been coding the following class and am wondering how the __get and __set functions should connect to the database in order to get the info from, and put the info into the database. The frustrating thing is I have done OOP at college with Java but it was a while ago and we never did any work with database integration.

Thanks in advance for any help people can give me.

<?php
class Item{

public $itName;
public $itId;
public $itClass;
public $itTime;
private $data;

function __construct(){

}

public function __set($name, $val){
$this->data[$name] = $val;
}
public function __get($name){
if (array_key_exists($name)) {
return $this->data[$name];
}
}

//generate html to display form
function displayForm() {
echo '
<form action="item.php" method="post">
<label for="itemName">Item Name</label>
<input type="text" name="itemName" /><br />
<label for="itemClass">Item Class</label>
<input type="text" name="itemClass" /><br />
<input name="submit" value="Submit" type="submit" /><input name="reset" value="Reset" type="reset" />
</form>';
}
}
?>

Dopple
06-17-2008, 04:13 PM
Sorry, just realised I had missed out the [PHP] tags and I can't edit the post.

NogDog
06-17-2008, 11:45 PM
I would probably not have the __get() and __set() interact with the DB. Rather, they would only interact with the $this->data array. Then I'd have separate methods for retrieving data from the DB to $this->data and for inserting/updating the DB from $this->data.

Dopple
06-18-2008, 02:04 AM
Perfect. That's what I was looking for. I think the php.net __get and __set examples should do me for the time being in that case. Thanks NogDog.