Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

So..

Name: Anonymous 2009-08-31 0:32

Learning OO PHP (inb4 *)

I was wondering what /prog/ thought of my database connection class? Oh, and I know that the interface isn't necessary, but it's a habit I picked up from my teacher.


<?php
interface Database
    {
    function __construct();
    function  __destruct();
    }
   
class Connection implements Database
    {
    private $mysqli;   
   
    private $host = "localhost";
    private $user = "root";
    private $pass = "saucepl0x";
    private $name = "content";
   
    public static function __construct()
        {
            $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
           
            if (mysqli_connect_errno())
                {
                printf ("Connection failed: %s\n", mysqli_connect_error())
                }
            else
                {
                return $this->mysqli;
                }
        }
       
    public static function  __destruct()
        {
        if ($this->mysqli)
            {
            $this->mysqli->close();
            }
        }
    }
?>


Is it okay? How can I improve on it?

Name: Anonymous 2009-08-31 0:42

I'm no Expert OO Programmer, but shouldn't your __construct() method return a value or throw an exception on error instead of just printing to screen.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List