Jump to content

[POO] Object constructor that uses mysql_fetch_object (opened)


Recommended Posts

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.

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 :D

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

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! :P

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.