flaab Posted May 12, 2007 Share Posted May 12, 2007 Good morning =) I'll go straight to the point. I'm developing a Mysql + Php website using Classes to manipulate each of the tables on my Db. So i thought that it would be cool if my constructor used mysql_fetch_object to create itself. Each object has the same attributes and in the same order that his db table. Here's the code of my class. class Texto extends Primitiva { var $cuerpo; /* * @ Contructor * @ Entrada: Identificador * @ Coge su tupla de la base de datos. */ function __construct($pid) { $result = mysql_query("SELECT * FROM `miljardines` WHERE id = '$pid' "); // Si la tupla no existe, lanzamos nuestro metodo error. if(mysql_num_rows($result) < 1) { parent::arrojar("No se ha podido crear objeto Texto. Tupla no existente."); } // Si existe, creamos un objeto temporal $this = mysql_fetch_object($result); } } But the interpreter crashes: "Cannot redefine $this". My question is...If i assigned mysql_fetch_object to a temporal object, how can i assign each of it's value to my own object? Thanks in advance. Cheers. Link to comment https://forums.phpfreaks.com/topic/51039-poo-object-constructor-that-uses-mysql_fetch_object-opened/ Share on other sites More sharing options...
neel_basu Posted May 12, 2007 Share Posted May 12, 2007 If you are using PHP 4 use function Texto($pid) Not __construct() Link to comment https://forums.phpfreaks.com/topic/51039-poo-object-constructor-that-uses-mysql_fetch_object-opened/#findComment-251176 Share on other sites More sharing options...
flaab Posted May 12, 2007 Author Share Posted May 12, 2007 Solved it this way =) /* * @ Clase Texto * @ Hereda esqueleto de primitiva */ class Texto extends Primitiva { // Variables var $cuerpo; /* * @ Contructor * @ Entrada: Identificador * @ Coge su tupla de la base de datos. */ function __construct($pid) { $result = mysql_query("SELECT * FROM `texto` WHERE id = '$pid' "); // Si la tupla no existe, lanzamos nuestro metodo error. if(mysql_num_rows($result) < 1) { parent::arrojar("No se ha podido crear objeto Texto. Tupla no existente."); } // Si existe, creamos un objeto temporal $temp = mysql_fetch_object($result); // Nos auto asignamos foreach($temp as $key => $value) { $this->$key = $value; } } } Thx anyway Link to comment https://forums.phpfreaks.com/topic/51039-poo-object-constructor-that-uses-mysql_fetch_object-opened/#findComment-251177 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 I've just adapted one of my classes. My $data was originally an array and I used mysql_fetch_array, but this works using fetch_object. <?php class document2 { public $category; public $doc_id; private $data; function __construct ($cat, $docid) { $this->category = $cat; $this->doc_id = $docid; $sql = "SELECT title, doc_type, date_published, author, loc_id FROM document WHERE category = '$cat' AND doc_id = '$docid' "; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); if (($row = mysql_fetch_object($res))!=false) $this->data = $row; else { $this->data = (object)array(); $this->data->title = 'new document'; } } public function __get ($item) { return $this->data->$item ; } public function __set ($item, $val) { $this->data->$item = $val; } } $obj = new document2 ('IS', 3); echo $obj->title; // --> Powerpoint training cd echo $obj->doc_type; // --> 5 $obj->doc_type = 3; echo $obj->doc_type; // --> 3 $obj = new document2 ('IS', 53); echo $obj->title; // --> new document echo $obj->doc_type; // --> $obj->doc_type = 3; echo $obj->doc_type; // --> 3 ?> Link to comment https://forums.phpfreaks.com/topic/51039-poo-object-constructor-that-uses-mysql_fetch_object-opened/#findComment-251190 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 I've never been a fan of classes/objects myself. I don't really know why, just always found them complicated. I should really find a couple of hours to read up and understand them but not had chance yet! preg_replace too ~ can't understand all the \/[.] stuff that goes on in it! Link to comment https://forums.phpfreaks.com/topic/51039-poo-object-constructor-that-uses-mysql_fetch_object-opened/#findComment-251191 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.