Author Topic: First crack at OOP and PHP  (Read 15369 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
First crack at OOP and PHP
« on: May 25, 2005, 03:01:37 PM »
I'm kinda bored (read "avoiding work") today so I wrote a PHP class to handle errors from MySQL and MS SQL queries.  I haven't tested the mysql end of it yet since we only use MS SQL here at work, but here's what I've got so far.. what do you guys think?

Code: [Select]
<?php
/* =========================================================
Class Name: sql_handler
Date: 05/25/2005 
Usage: Query Error Handling and elegant error reporting
Notes: $db: 1 = mssql, 2 = mysql
========================================================= */
class sql_handler 
{
var $mail_msg;
var $db_type;
var $query;
var $page;
var $error;

function sql_handler($db$query$page)
{
$this->db_type $db;
$this->query $query;
$this->page $page;
}

function treat_mssql_error($buffer)
{
$buffer ereg_replace("<b>Warning</b>:  MS SQL message:","<b>Error in query (SQL Server)</b>: "$buffer);
$buffer explode("(severity"$buffer);
return $buffer[0];
}

function run_query()
{
if($this->db_type == 1)
{
ob_start();
$result mssql_query($this->query);
if(!$result)
{
echo ob_get_contents();
$this->error $this->treat_mssql_error(ob_get_contents());
ob_end_clean();
$this->mail_msg "There was an error querying the MS SQL Server on page: " $this->page ".  An error message has been shown to the user.";
$this->mail_error();
return false;
}
ob_end_clean();

else if($this->db_type == 2)
{
$result mysql_query($this->query);
if(!$result)
{
$this->error mysql_error();
$this->mail_msg "There was an error querying the MySQL Server on page: " $this->page ".  An error message has been shown to the user.";
$this->mail_error();
return false;
}
}
return $result;
}

function mail_error()
{
/* recipients, change to suit */ 
$to  "bennet.oberholzer@volvo.com";
$subject "Error Report"

/* HTML mail Content-type header. */ 
$headers  "MIME-Version: 1.0\r\n"
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"

/* additional headers */ 
$headers .= "To: Admin <" $to ">, \r\n"
$headers .= "From: DB Server <sqladmin@donotreply.com>\r\n"

/* HTML mail message */ 
$message 
<html> 
<head> 
</head> 
<body> 
<p style="color:red;font-weight:bold;font-family:Tahoma;font-size:8pt">' 
$this->mail_msg '</p> 
<p style="font-family:Tahoma;font-size:8pt">The query was: ' 
$this->query '</p> 
<p style="font-family:Tahoma;font-size:8pt">The error was: ' 
$this->error '</p> 
</body> 
</html> 
'


$x = @mail($to$subject$message$headers); 
}
}
?>
« Last Edit: August 18, 2010, 07:09:35 PM by ober »

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
First crack at OOP and PHP
« Reply #1 on: June 18, 2005, 03:43:31 AM »
No offense, but it's not very OO.  Check out phppatterns.com for some solid examples of OOD in php.  Most of the content is dated, but still relavent.  Also there are some ORM frameworks that are becoming popular with php5...check out http://wiki.cc/php/Object_Relational_Mapping for more info.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
First crack at OOP and PHP
« Reply #2 on: June 18, 2005, 09:37:00 AM »
Unfortunately, most of my hosts are still running 4, so I haven't gotten a chance to work with 5.  And I realize it's not "very OO"... but it does what I need it to.  It's like a fancy function for me.

Hillbillie

  • Jackass II
  • Posts: 69
  • Karma: +11/-0
First crack at OOP and PHP
« Reply #3 on: June 21, 2005, 03:09:40 PM »
I'm almost embarassed to say that I still don't know classes. :o Classes, shmasses...
Reduced fat and now 100% pot free, but same great okay taste.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
First crack at OOP and PHP
« Reply #4 on: June 21, 2005, 04:10:58 PM »
I never really had a reason to learn them... which is why I'm just now picking them up.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
First crack at OOP and PHP
« Reply #5 on: June 22, 2005, 09:40:42 AM »
I tried to do some OO in PHP before but I find it too much a pain (in PHP 4).

Maybe if I get PHP 5 working on my test box I'll try again

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
First crack at OOP and PHP
« Reply #6 on: June 22, 2005, 12:53:19 PM »
Is it really that bad?  I don't see that much difference between C++ classes and this.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
First crack at OOP and PHP
« Reply #7 on: June 22, 2005, 04:31:52 PM »
No data protection (ie no private part), everything was done with the annoy ->, didn't seem to support very well things like:
obj->func()->bleh()