View Single Post
Old 07-13-2008, 08:25 PM   #1 (permalink)
CMellor
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default My First Class - Any Good?

Hey,

I've never coded using PHP 5's OOP, and while I was planning a new project, I knew I was going to be using it, as well as using the MySQLi function. I've read loads of stuff now on OOP and I think I've got the hang of it, sorta, but at a newbie stage... ya know? lol

I whipped up this code, I just want you to tell me what I did right and wrong and how it can be improved:

PHP Code:
<?php
class math {

    const 
host 'localhost';
    const 
user 'root';
    const 
pass 'blergh';
    const 
db 'project';
    
    public function 
__construct() {
        
// Start MySQLi connection
        
$mysqli = new mysqli(self::hostself::userself::passself::db);
        
// Check if connect
        
if(mysqli_connect_errno()) {
            throw new 
exception('MySQLi did not connect');
        }
    }
        
    public function 
plus($x$y) {
        
$this->$x;
        
$this->$y;
        
        return 
$this->$this->y;
    }

}

$class = new math;

try {
    echo 
$class->plus(2255);
}
catch(
exception $e) {
    echo 
$e->getMessage();
}

?>
Cheers!
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote