Class file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Class file

Post by shivam0101 »

Hi,

I am creating a class file. I wanted to know whether its correct or any modification has to be done.

Thanks

CODE:

Code: Select all

<?php

class Test
{
  var $conn;
  
 function Db($host, $username, $password, $database)
 {
	$this->conn=@mysql_ect($host, $username, $password);
	if($this->conn)
	{
		$con_db=mysql_select_db($database, $this->conn);
		if($con_db)
		return $con_db;
		else
		return mysql_error();
	}
		
	else
	{
		return mysql_error();
	}
	
 }
 
 
 function DbClose()
 {
    $con_close=mysql_close($this->conn);
	if($con_close)
		return $con_close;
 } 


 function Fetch($tableNames, $cond=null, $fields)
 {
	$query_select=mysql_query("SELECT * FROM $tableNames $cond",$this->conn);
	if(mysql_num_rows($query_select)>0)
	{
		while($fetch=mysql_fetch_array($query_select))
		{
		  foreach($fields as $field)
		  {
			$array_res[$field]=$fetch[$field];
		  }
		}
	   return $array_res;
    }
	else
	{
	   return 'No results Found';
	}	
 }

 
 function Count($tableNames, $cond=null)
 {
	$query_count=mysql_query("SELECT * FROM $tableNames $cond",$this->conn);
	if($query_count)
	{
		$num_rows=mysql_num_rows($query_count);
		return $num_rows;
	}
	else
	{
	   return mysql_error();
	}
 }

 
 function Image($memberId)
 {
    $fields=array('Thumbnail');
	$get_res=$this->GetValues("products,products_photo", "WHERE clients.CID='$memberId' AND clientimage.CID=clients.CID", $fields);
  
    return $get_res;
 }




 function GetMatchesToApproveCount($memberId)
 {
    $count_resp=$this->GetCount("products", "WHERE report!='Reject' AND SClientID=$memberId AND (LP='Approved' AND SecondClientProposal='InitiateProposal')");
    $count_sug=$this->GetCount("matches", "WHERE report_card_status='No' AND ClientID=$memberId");
	
	return $count_resp+$count_sug;
 }

 
 
 
 
 function Introductions($memberId)
 {
     $intro_count=$this->GetCount("products", $cond="stage_id=0 AND report!='Reject' AND LCP='Approved' AND SCP='Approved' AND (LCID=$ClientID OR SClID=$ClientID)");
	 return $intro_count;
 }
 
 
 
 functionPaymentsCount($memberId)
 {
    $p="products";
	$pe="product_cost";
	
	$count_pmts=$this->GetCount("$p, $pe", "WHERE $p.report!='Reject' AND ($p.LCID=$memberId OR $p.SCID=$memberId) AND $p.LP='Approved' AND $p.SCP='Approved' AND $p.PID=$pe.PID AND $pe.EFinalDate!='0000-00-00' AND $pe.reschedule_date='0000-00-00' AND $memberId NOT IN (SELECT client_id FROM pevents WHERE pid=$p.PID)");
    return $count_pmts;
 }

 
 
 
 function Photo($memberId)
 {
     $photo_req_count=$this->GetCount("clients_req_photos", $cond="WHERE request_id=$memberId AND flag=''");
	 return $photo_req_count;
 }


}
?>




?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

To what end are you referring to with "any modification"? What is the purpose of this class?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

If you want general criticism there is a specialist forum for that.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Hello feyd,

This class is for a project (Not a general class file). The project has allready been done followin procedural type. The client want a second version using class files.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Okay....

It's a very very very basic class. It's barely OOP. I think some refactoring is in order. There are certainly a few efficiency issues. Count() for example.

Typically a database connection class is followed by query classes and result set classes. Sometimes even record classes.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

This is my first project related to OOP.

1. I was asked to follow the naming conventions presented in this link http://www.dagbladet.no/development/php ... classnames

I am bit confused, which is variablename and which is attribute name

In the code,

Code: Select all

function Db($host, $username, $password, $database) 
 { 
        $this->conn=@mysql_ect($host, $username, $password); 
        if($this->conn) 
        { 
                $con_db=mysql_select_db($database, $this->conn); 
                if($con_db) 
                return $con_db; 
                else 
                return mysql_error(); 
        } 
                
        else 
        { 
                return mysql_error(); 
        } 
        
 }
which is attribute name?

If you suggestions to imporve the class file, would be more helpful
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The "standards" that you are referring too look more like C++ than PHP.

In C++, "attributes" (member variables) are prefixed with "m_", and in PHP, they are prefixed with "_". The standards you are looking at only use "m" which hardly seems like a standard to me. Also, their function naming is C++ style, as in C++, function names star with an uppercase letter. In PHP (by the standards), they do not.
It sounds as though they wanted to establish a standard, but didn't.


Anyway, a variable name is just a variables name. The variable name of $bigNumber is "bigNumber." An attribute name is the name of a member variable.
I'm not sure who sent you to that link, but I think you're looking at the wrong stuff.


And as for your class... It seems so.. procedural. I don't see any communication and there's only one member variable. You have one class that merges a database class with.. uh... something else. I think you should rethink this code.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah those are silly standards use the ones in my sig.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just to clarify a few things that have been said..

C++ and PHP do not have any formal coding standards outside of general syntax and identifier naming. You can use any name you wish for any identifier provided it conforms.

So while some in C++ use "m_," not everyone does. In fact, provided there is a convention for naming, it's a possible standard -- whether it makes sense or not.

So unless you can convince the person who told you to use this standard, I wouldn't just start using another.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:Just to clarify a few things that have been said..

C++ and PHP do not have any formal coding standards outside of general syntax and identifier naming. You can use any name you wish for any identifier provided it conforms.

So while some in C++ use "m_," not everyone does. In fact, provided there is a convention for naming, it's a possible standard -- whether it makes sense or not.

So unless you can convince the person who told you to use this standard, I wouldn't just start using another.
I use the same naming conventions for both languages (almost). I never liked the "m" or the single "_," so I use __ (two underscores) for member variables. Sadly, PHP5+ is planning on killing me on that too with the "magic" methods and constants.

The only real difference is that in C++, I used prefixes (forgot what we called them) to quickly identify the data type of the variable. However, in PHP, since the types are so flexible, I just have to make more descriptive variable names.


I got bored and started doing some C++ programming again and gahh. Too many PHP habits. The "function" keyword kept on popping up.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:The only real difference is that in C++, I used prefixes (forgot what we called them) to quickly identify the data type of the variable. However, in PHP, since the types are so flexible, I just have to make more descriptive variable names.
It's normally called Hungarian Notation. It's a really bad habit.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Hungarian! Right. :-D

It's bad? Microsoft supports it to fullest in all of their code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:It's bad? Microsoft supports it to fullest in all of their code.
They've been moving away from it for years.

It's bad because you shouldn't need to care too much about the data type. The name should simply be descriptive enough. What if you suddenly need to switch from integer to double? Now you have to rename all the references. Sometimes that's easy, sometimes it's not so easy.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Iam trying to improve the code. In the query, if there are join statements i will be using alias for table names, since the same table name repeats in several methods, i want to assign table name to a variable, which i will be using as alias.

for example

Code: Select all

<?php

$p='products';

?>
by doing this, i cannot access without $this operator?. Is there any other way to access the value? because i do not want to use $this-> in front of every alias.

I am still not clear about the difference between variable name and attribute.

Pls tell me which one is variable and which one is attribute in the bellow code, which will be clear.

Code: Select all

class Test
{
   var $con;

   function getvalue($table_name)
   {
       $res=execute_query($table_name);
   }

}
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Class file

Post by BDKR »

shivam0101 wrote:Hi,

I am creating a class file. I wanted to know whether its correct or any modification has to be done.

Thanks

CODE:

Code: Select all

<?php

class Test
{
  var $conn;
  
 function Db($host, $username, $password, $database)
 {
	$this->conn=@mysql_ect($host, $username, $password);
	if($this->conn)
	{
		$con_db=mysql_select_db($database, $this->conn);
		if($con_db)
		return $con_db;
		else
		return mysql_error();
	}
		
	else
	{
		return mysql_error();
	}
	
 }
 
 
 function DbClose()
 {
    $con_close=mysql_close($this->conn);
	if($con_close)
		return $con_close;
 } 


 function Fetch($tableNames, $cond=null, $fields)
 {
	$query_select=mysql_query("SELECT * FROM $tableNames $cond",$this->conn);
	if(mysql_num_rows($query_select)>0)
	{
		while($fetch=mysql_fetch_array($query_select))
		{
		  foreach($fields as $field)
		  {
			$array_res[$field]=$fetch[$field];
		  }
		}
	   return $array_res;
    }
	else
	{
	   return 'No results Found';
	}	
 }

 
 function Count($tableNames, $cond=null)
 {
	$query_count=mysql_query("SELECT * FROM $tableNames $cond",$this->conn);
	if($query_count)
	{
		$num_rows=mysql_num_rows($query_count);
		return $num_rows;
	}
	else
	{
	   return mysql_error();
	}
 }

 
 function Image($memberId)
 {
    $fields=array('Thumbnail');
	$get_res=$this->GetValues("products,products_photo", "WHERE clients.CID='$memberId' AND clientimage.CID=clients.CID", $fields);
  
    return $get_res;
 }




 function GetMatchesToApproveCount($memberId)
 {
    $count_resp=$this->GetCount("products", "WHERE report!='Reject' AND SClientID=$memberId AND (LP='Approved' AND SecondClientProposal='InitiateProposal')");
    $count_sug=$this->GetCount("matches", "WHERE report_card_status='No' AND ClientID=$memberId");
	
	return $count_resp+$count_sug;
 }

 
 
 
 
 function Introductions($memberId)
 {
     $intro_count=$this->GetCount("products", $cond="stage_id=0 AND report!='Reject' AND LCP='Approved' AND SCP='Approved' AND (LCID=$ClientID OR SClID=$ClientID)");
	 return $intro_count;
 }
 
 
 
 functionPaymentsCount($memberId)
 {
    $p="products";
	$pe="product_cost";
	
	$count_pmts=$this->GetCount("$p, $pe", "WHERE $p.report!='Reject' AND ($p.LCID=$memberId OR $p.SCID=$memberId) AND $p.LP='Approved' AND $p.SCP='Approved' AND $p.PID=$pe.PID AND $pe.EFinalDate!='0000-00-00' AND $pe.reschedule_date='0000-00-00' AND $memberId NOT IN (SELECT client_id FROM pevents WHERE pid=$p.PID)");
    return $count_pmts;
 }

 
 
 
 function Photo($memberId)
 {
     $photo_req_count=$this->GetCount("clients_req_photos", $cond="WHERE request_id=$memberId AND flag=''");
	 return $photo_req_count;
 }


}
?>




?>
In terms of Responsibility Driven Design (or what people are more recently calling "Seperation of Concerns" LOL), this class needs some more thought. It starts out focusing on db interaction and then starts throwing in the kitchen sink. Good OOD (Object Oriented Design) would suggest that differing groups of functionality have their own classes.
Post Reply