Here is a snippet from my PHP book ;)
This is done using an pure PHP OO abstraction layer.
It is alright as far as interfaces go.
(Note.. most people use a DB abstraction layer these days in PHP)
.... long code snippet follows ....
/* Include the database abstraction class */
require_once "DB.php";
/* Static method to parse the DSN connection string into an array */
$dsn = DB::parseDSN("pgsql://[EMAIL PROTECTED]/MasteringPHP");
/* Static factory method to create pgsql DB object */
$db_obj = DB::factory("pgsql");
/* Connect to the databse */
if($db_status = $db_obj->connect($dsn))
{
echo "Error: " . $db_status->message . "<br>";
echo "Code: " . $db_status->code . "<br>";
}
/* Store the query to be executed in a variable */
$pets_query = "SELECT P.PetName,
O.FirstName,
O.LastName,
PT.CommonName
FROM Pet P
INNER JOIN Owner O ON O.OwnerID = P.OwnerID
INNER JOIN Pet_Type PT ON PT.PetTypeID = P.PetTypeID
ORDER BY LastName;";
/* Execute query */
$pets_result = $db_obj->simpleQuery($pets_query);
/* Begin table to format data */
$pet_tbl = "<table border=\"1\">\n";
$pet_tbl .= "<tr><td align=\"center\" colspan=\"4\"
bgcolor=\"#ffff99\">\n";
$pet_tbl .= "<strong>Pets and Owners</strong>\n";
$pet_tbl .= "</td></tr>\n";
$pet_tbl .= "\t<tr>\n";
$pet_tbl .= "\t\t<td>Pet Name</td>\n\t\t<td>Common Name</td>\n";
$pet_tbl .= "\t\t<td>Owner First Name</td>\n\t\t<td>Owner Last
Name</td>\n";
$pet_tbl .= "\t</tr>\n";
while($row = $db_obj->fetchRow($pets_result, DB_FETCHMODE_ASSOC))
{
$pet_tbl .= "\t<tr>\n";
$pet_tbl .= "\t\t<td>" . $row["petname"] . "</td>\n";
$pet_tbl .= "\t\t<td>" . $row["commonname"] . "</td>\n";
$pet_tbl .= "\t\t<td>" . $row["firstname"] . "</td>\n";
$pet_tbl .= "\t\t<td>" . $row["lastname"] . "</td>\n";
$pet_tbl .= "\t</tr>\n";
}
$pet_tbl .= "</table>\n";
$db_obj->disconnect();
?>
.....
Compared to..
<cfquery name="..." datasource="...">
SELECT P.PetName,
O.FirstName,
O.LastName,
PT.CommonName
FROM Pet P
INNER JOIN Owner O ON O.OwnerID = P.OwnerID
INNER JOIN Pet_Type PT ON PT.PetTypeID = P.PetTypeID
ORDER BY LastName
</cfquery>
<cfloop query="...">
<cfoutput>
<!---
Everyone knows how this goes so I am not going to waste
more bits
--->
</cfoutput>
</cfloop>
Ehh, the PHP isn't much prettier is it? :) Not quite so bad as working with
a native DB function set in PHP, but still most people would consider the
PHP to be less pleasant. PHP is very declarative and borrows from languages
like C, Perl, and Java so it is going to be more like those languages. It
isn't tag based so it can't borrow from the simplicity of markup. Although
PHP offers a lot of syntactical sugar that I often miss in CF (For the same
reason I miss it in say... Java). And I do think the OO framework in PHP is
more mature in some (functional) respects.
The thing that always puts me off about PHP, and what will always make it be
a second class/web language in my mind is how the language is "glued"
together. Oh, want access to images? Lets wire in the GD library API. Ehh,
XML? Ok, no problem, lets mimic the C API for that in PHP. PDF, ok. You end
up with a lot of C API replicas in PHP. So there are a few dozen disparate
APIs you end up working with in PHP. The contradictions even go through the
core of the language. Basic things like directory and file manipulation are
allowed to be different and inconsistent in how they are performed. (I
understand the Perl TIMTOWTDI philosophy, but I see things in PHP as sloppy
rather more than anything else.)
CF is more consistent across the board. CF has more vision and scope than
PHP ever will have. PHPs solution to enterprise level scalability concerns
are to off-load it to a separate project. There are things CF has supported
forever that I still want in a stable form in PHP. (Such as an "Application"
scope for PHP). The PHP answer to this is http://www.vl-srm.net, a
completely separate daemon. :)
Anyway, this post is long. Needless to say I think CF is a more complete
solution and it is only getting better, but it is silly to dismiss PHP off
hand due to the amount of almost built-in ready to go libraries available.
You can always implement your own OO oriented interfaces to any libraries
you use, but it would be nice for things to come out of the box consistent.
Jeremy
-----Original Message-----
From: Sean A Corfield [mailto:[EMAIL PROTECTED]
Sent: Thursday, February 27, 2003 5:13 PM
To: CF-Talk
Subject: Re: PHP versus CF Development Speed?
On Thursday, Feb 27, 2003, at 11:30 US/Pacific, Alexander Sherwood
wrote:
> At 02:28 PM 2/27/2003 -0500, you wrote:
>> Anyone have any good case studies or articles explaining development
>> time
>> coparisons between CF and PHP? I've statistics stating ASP takes 60%
>> more
>> dev time than CF. Anyone have any numbers comparing CF to PHP?
>
> At least 60%, I would imagine!
Depends on how complex your app is I guess and whether you use a
framework (you can use Fusebox with PHP, for example). Database access
code is substantially uglier in PHP than in CF:
@ $link = mysql_pconnect( $hostname, $username, $password );
if ( $link ) {
@ $res = mysql_select_db( $dbname, $link );
$res = mysql_query( 'select * from category where name like
"%'.$search.'%" '.
'or description like "%'.$search.'%" order by name
asc', $link );
if ( $res ) $nrows = mysql_num_rows( $res );
if ( $nrows > 0 ) {
for ( $i = 0; $i < $nrows; ++$i ) {
$row = mysql_fetch_row( $res );
if ( $row[2] != '' )
... column 2 is not empty ...
}
}
}
This is code from my own site - I'm not the world's greatest PHP
programmer so there may be better ways to do this!
Sean A Corfield -- http://www.corfield.org/blog/
"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription:
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Signup for the Fusion Authority news alert and keep up with the latest news in
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4