6 replies
Okay, well this is the first time i have really used any OOP in PHP, im surprised that i havn't even touched on it.

Anyway, the problem is i dont totally understand what i am doing, i think i got the basics sort of figured out, this is what i came up with when i try making a DB class which at the moment connects to the database.

PHP Code:
class Db {
    
    function 
__construct($db_host$db_user$db_password$db_name){
        
        
//* Connect to MySQL (@ = Suppress Errors)
        
$this->db_handler = @mysql_connect($db_host$db_user$db_password);
        
        if(!
$this->db_handler)
            echo 
"Didn't Work";
        else
            echo 
"Did Work";
        
    }

}

$new = new Db('localhost''root''''test'); 
Is this right?, Is there anything here that i should have, or i shouldn't have?
--
Also, i read that __contruct() doesnt work in PHP 4, but i can make a function with the same name as the class, so can i do this:

PHP Code:
function Db($db_host$db_user$db_password$db_name){
    
__construct($db_host$db_user$db_password$db_name);

Or do i have to do:
PHP Code:
function Db($db_host$db_user$db_password$db_name){
    
$this->__construct($db_host$db_user$db_password$db_name);

Or something else?
---
I figured if i just dive into this and try making something, that i would learn it faster.
#oop #php
  • Profile picture of the author Christopher Airey
    What you have looks fine. I didn't test it, but I don't see any problems with it. As for the PHP4 constructor, there's no function with the name __constructor(), so you would name the function as the class. I haven't done anything PHP4 related in like 5-6 years, but it should look like:

    PHP Code:
    class Db 
         
        function 
    Db($ db_host, $ db_user, $ db_password, $ db_name) { 
        } 

    {{ DiscussionBoard.errors[2140860].message }}
  • Yeah thats what i read somewhere that PHP4 didnt support __construct, so i made a function with the same name as the class, and that function called the __contruct function i set up for PHP5.

    Do you think i should just ditch PHP4, considering its been what like 2 years since all support for it ended?
    {{ DiscussionBoard.errors[2140938].message }}
  • Profile picture of the author Christopher Airey
    I didn't get what you were talking about with the construct at first, but what you have should work. You'd do $this->__construct().

    There's honestly no reason to stick with PHP4 other than those cases where you're forced to. PHP5 is better as a language, and if I can recall, it's also a lot faster than PHP4.
    {{ DiscussionBoard.errors[2141189].message }}
  • Profile picture of the author Aaron Sustar
    You should definitely forget about PHP4, because (unlike Internet Explorer 6 and stuff like that) it's something you have control over - simply install PHP5 on your server and forget about all headaches that come with PHP4.

    What you've written so far seems okay and should definitely be working, so - good luck on writing your first PHP class.

    DB classes sure come in handy when you have to switch to another database. I also suggest you add an optional argument to the constructor, like this:
    function Db($db_host, $db_user, $db_password, $db_name, $debug_mode = false) {
    ...
    }

    When the $debug_mode is activated, you can always output the query before executing it, and you'll be able to find various SQL errors much quicker this way.
    {{ DiscussionBoard.errors[2162444].message }}
    • Profile picture of the author mgkimsal
      I'm a bit late to the game here, but I'd suggest you don't write your own. Use the built-in PDO in PHP5.

      $d = new PDO("mysql:host=localhost,dbname=foo","username"," password");
      $rows = $d->query("select * from foobar")->fetchAll();

      Doing that off the top of my head - might be not 100% right, but go look up PDO. It's faster and more battle-tested than anything you'll be able to write up yourself.
      {{ DiscussionBoard.errors[2164310].message }}
  • Oh, im only writing my own for educational purposes, i figure if i write my own then ill learn more about OOP than if i use one that already exists.
    --
    I was recommended to use the mysqli class in PHP5, whats the PDO one like?, is it better?
    {{ DiscussionBoard.errors[2168090].message }}

Trending Topics