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

Pages: 1-

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:37

Defining constructor/destructor in the interface? That's enterprise-class.

You might want to stop working on this and use an existing PHP library that aspires to be JDBC. That, or use a persistence library. Don't be gay like everyone else and use mysqli, or even MySQL at all for that matter. Be a straight.

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.

Name: Anonymous 2009-08-31 0:44

>>3
You're right. I forgot to add exit(); after the error message.

Name: Haxus the SQLite 2009-08-31 0:45

SQLite in the house

Name: Anonymous 2009-08-31 1:23

Defining constructor/destructor in the interface? Does that even work? I thought they were implicit for all classes.

>>2-san is retarded. JDBC is the bane of any Java programmer.
>>3-san is similarly retarded. Throwing an exception in a constructor is just about the worst thing you can do, whether it causes problems or not. A constructor should similarly, not return a value, at least from an OO perspective. In fact, returning a value in a PHP constructor as far as I am aware is completely pointless. Unless that is, you are calling the constructor manually which completely defeats the purpose of having it in the first place. I suggest you go back to basics and read SICP, upon whose completion you might find yourself better prepared to understand real OO programming in a real OO language - like Smalltalk.

Name: Anonymous 2009-08-31 1:45

>>6
Yeah, it works, but it's not necessary.

Thank you, Anon-kun for your post.

Name: Anonymous 2009-08-31 1:54

>>3
this post is awful

He's helping the stupid OP with bullshit homework/work crap whatever the fuck. He's using prog 'memes' as well because thats what everyone else does. This kind of person makes me fucking despair.

Name: Anonymous 2009-08-31 1:56

>>6-tard has never had to write db-interoperable code. He spends most of his time expert programming in functional academic toy languages.

Name: Anonymous 2009-08-31 2:13

>>9
I liked the part where you justified your views by attacking my character without any basis.

Name: Anonymous 2009-08-31 2:22

>>6
I LOVE YOU! I LOVE YOUR POST! I READ IT 5 TIMES! KEEP POSTING!

Name: sage 2009-08-31 2:28

Funny how no one has told OP that constructors/destructors cannot be static.

Name: Anonymous 2011-02-03 2:44

Name: Anonymous 2011-02-03 3:37

<

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