Page 1 of 2
Class file
Posted: Sat Jun 09, 2007 12:27 am
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;
}
}
?>
?>
Posted: Sat Jun 09, 2007 4:11 pm
by feyd
To what end are you referring to with "any modification"? What is the purpose of this class?
Posted: Sat Jun 09, 2007 6:30 pm
by Ollie Saunders
If you want general criticism there is a specialist forum for that.
Posted: Mon Jun 11, 2007 12:12 am
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
Posted: Mon Jun 11, 2007 12:28 am
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.
Posted: Mon Jun 11, 2007 2:00 am
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
Posted: Mon Jun 11, 2007 8:15 am
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.
Posted: Mon Jun 11, 2007 2:44 pm
by Ollie Saunders
Yeah those are silly standards use the ones in my sig.
Posted: Mon Jun 11, 2007 3:21 pm
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.
Posted: Mon Jun 11, 2007 3:31 pm
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.
Posted: Mon Jun 11, 2007 3:37 pm
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.
Posted: Mon Jun 11, 2007 3:42 pm
by superdezign
Hungarian! Right.
It's bad? Microsoft supports it to fullest in all of their code.
Posted: Mon Jun 11, 2007 3:48 pm
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.
Posted: Tue Jun 12, 2007 1:47 am
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
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);
}
}
Re: Class file
Posted: Tue Jun 12, 2007 8:50 am
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.