Jump to content

Recommended Posts

<?php
class ItemVote extends ConnectDB
{
private $table;
private $id;
private $item;
private $que;
private $res;
private $newval;

function getItem($table, $id, $item)
{
 $this->table=$table;
 $this->id=$id;
 $this->item=$item;
}

function getData()
{
  $this->que="select $this->item from $this->table where $this->id";
  $this->res=mysql_query($this->que);
  $this->showResult();	
}

function manipulateData()
{
  $this->newval=$this->row[0]++;	
}

function insertData()
{
  $this->que="update $this->table set $this->item=$this->newval where $this->id";
  $this->res=mysql_query($this->que);	
}
}
?>

 

is a constructor neccessary for the above class ?

 

this is ConnectDB, the class's above parent class:

 

<?php
class ConnectDB
{
private $localhost;
private $user;
private $pass;
private $db;
private $con;
private $data;
private $que;
//private $res;

function __construct($l, $u, $p, $d)
{
  $this->localhost=$l;
  $this->user=$u;
  $this->pass=$p;
  $this->db=$d;	
}

function connectdb()
{
  $this->con=mysql_connect($this->localhost, $this->user, $this->pass);
  $this->data=mysql_select_db($this->db, $this->con);	
}

function queryMachine($query)
{
	$this->que=$query;
	$this->res=mysql_query($this->que);
	//$this->row=mysql_fetch_assoc($this->res);
}

function showResult()
{
  
}
?>

Firstly,

 

Please don't extend a db class unless the class you are extending also has a lot to implement in db access requirements.

 

If your processing scripts or logic needs to do db access you can use an instance of the db class. and you can always have a singleton instance of the db class if your requirements suggest.

 

I strongly feel data classes (if implemented as classes at all.. which is good) should be isolated from other classes and only arguments passed to methods to do the required.. not extend the data class in every other class.

 

Extending means, you are expecting to use all methods of the data class, its variables and further going to add on it.

 

To your question : do you need a constructor?

 

like already mentioned by another user here.. it is not mandatory but a best practice even if it is empty.

 

and you will need a constructor if you want to set values to some variables inside your class upon instancing it.

 

for example, if we have a user class with a user id private variable.

if makes lot of sense to set the user id variable with a value as soon as you instance the class.

 

regards,

Harish.

www.floresense.com

www.harishpalaniappan.com

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.