Jump to content

Can u pls check if my classes are good, am i doing it doing the good way ?


jd2007

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.