jd2007 Posted August 19, 2007 Share Posted August 19, 2007 <?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() { } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65661-do-i-need-a-constructor-for-the-code-below/ Share on other sites More sharing options...
emehrkay Posted August 19, 2007 Share Posted August 19, 2007 no, but it is good practice to have one. it could be blank. it is also good practice to have a __destruct() method. in your db class, you'd use to to disconnect from the database Quote Link to comment https://forums.phpfreaks.com/topic/65661-do-i-need-a-constructor-for-the-code-below/#findComment-328019 Share on other sites More sharing options...
Daniel0 Posted August 19, 2007 Share Posted August 19, 2007 Just remember to call the parent constructor (or destructor) if you override it in a child class, unless you want to completely override it, but that's not the case in your posted code. Quote Link to comment https://forums.phpfreaks.com/topic/65661-do-i-need-a-constructor-for-the-code-below/#findComment-328049 Share on other sites More sharing options...
pl_harish Posted August 27, 2007 Share Posted August 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65661-do-i-need-a-constructor-for-the-code-below/#findComment-335193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.