jd2007 Posted August 19, 2007 Share Posted August 19, 2007 ConnectDB class: <?php /** * @author * @copyright 2007 */ 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() { } } ?> Title class and RightInfo class is here: <?php /** * @author * @copyright 2007 */ include("connectdb.php"); class Title extends ConnectDB { private $table; private $id; private $title; private $que; private $row; function selectTitle($table, $id, $title) { $this->table=$table; $this->id=$id; $this->title=$title; $this->que="select $this->title from $this->table where id='$this->id'"; $this->queryMachine($this->que); } function showResult() { $this->row=mysql_fetch_row($this->res); echo $this->row[0]; } } class RightInfo extends ConnectDB { private $table; private $id; private $author; private $submitted; private $score; private $size; private $que; private $arr; private $i; private $info; private $row; function selectInfo($author, $submitted, $score, $size) { $this->author=$author; $this->submitted=$submitted; $this->score=$score; $this->size=$size; $this->que="select $this->author, $this->submitted, $this->score, $this->size from $this->table where id='$this->id'"; $this->queryMachine($this->que); } function showResult() { $this->arr=array("Author", "Submitted", "Score", "Size"); $this->row=mysql_fetch_array($this->res); $this->i=0; foreach ($this->row as $this->info) { if ($this->i==2) { echo $this->info[0]." : ".$this->arr[$this->i]." / 5"; $this->i++; } else { echo $this->info[0]." : ".$this->arr[$this->i]; $this->i++; } } } } ?> Title and RightInfo extends ConnectDB. Title searches a mysql table for the title for a flash and displays the title. RightInfo searches a mysql table for author's name, submitted date, flash's score, and size and displays it. pls tell me if the classes are coded properly. in Title and RightInfo, these variables repeat: $table, $id, $que, $row repeat...give me an idea what to do if repeating them is bad. is it bad ?...i'm a newbie to oop..pls check... Quote Link to comment https://forums.phpfreaks.com/topic/65658-can-u-pls-check-if-my-classes-are-good-am-i-doing-it-doing-the-good-way/ Share on other sites More sharing options...
emehrkay Posted August 19, 2007 Share Posted August 19, 2007 so far it looks good, a few things though: 1. if you call one of the children, your parent never gets the db info; user, pass, etc. maybe you should just assign those values as properties. 2. you do not set the visibility in the parent class. even though it defaults to public, you should still type it. maybe you should make them all protected so that the only way someone can access it is by calling the class directly or extending it. 3. for the repeating properties, and methods, you could either use abstract methods/properties or just define them in the parent. if you use abstract in the parent, that means that you'd have to use those in all of the children and they could do differnet things. for example you could add abstract funtion insert(); inside of the parent. that would mean that all of the children would have to have a public insert method. for the repeating properties, you could set them up in the parent as protected 4. minor and a design choice, typically one class per file Quote Link to comment https://forums.phpfreaks.com/topic/65658-can-u-pls-check-if-my-classes-are-good-am-i-doing-it-doing-the-good-way/#findComment-328018 Share on other sites More sharing options...
Daniel0 Posted August 19, 2007 Share Posted August 19, 2007 1. if you call one of the children, your parent never gets the db info; user, pass, etc. maybe you should just assign those values as properties. He just needs to declare it protected instead of private, then it'll be accessible by descendants as well. Quote Link to comment https://forums.phpfreaks.com/topic/65658-can-u-pls-check-if-my-classes-are-good-am-i-doing-it-doing-the-good-way/#findComment-328054 Share on other sites More sharing options...
emehrkay Posted August 19, 2007 Share Posted August 19, 2007 1. if you call one of the children, your parent never gets the db info; user, pass, etc. maybe you should just assign those values as properties. He just needs to declare it protected instead of private, then it'll be accessible by descendants as well. yeah but if keeps the same setup where the constructor in the parent defines the db info, it would be difficult to just call the children class because he'd have to somehow do parent::_constructor($login_vars...) within every child's constructor. so doing $x = new selectTitle($table, $id, $title); would seem like a chore because he'd have to add the db login info as well to that constructor. just my take Quote Link to comment https://forums.phpfreaks.com/topic/65658-can-u-pls-check-if-my-classes-are-good-am-i-doing-it-doing-the-good-way/#findComment-328086 Share on other sites More sharing options...
Daniel0 Posted August 19, 2007 Share Posted August 19, 2007 Hmm... true. It would perhaps be better to just pass a ConnectDB object to the constructor of the other classes that needs it than to extend the ConnectDB class function __construct(ConnectDB $db, $author, $submitted, $score, $size); Quote Link to comment https://forums.phpfreaks.com/topic/65658-can-u-pls-check-if-my-classes-are-good-am-i-doing-it-doing-the-good-way/#findComment-328174 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.