Jump to content

OOP noob looking for guidence!


iPixel

Recommended Posts

I'm trying to start and shift my programming towards OOP style as opposed to procedural. Mainly for cleanliness. But whatever the reason i want to learn it =).

 

So i'm making this easy breeze MySQL connection class but the page wont even open properly.

Any help/guidence is much appreciated.

 

<?php
# OOP LEARNING PAGE #

class DBcon()
{

	function DBcon($username,$password)
		{

			$db = mysql_connect("localhost", $username, $password) or die(mysql_error()); 

			if(mysql_select_db("tablename",$db) or die(mysql_error()))
				{ echo "Success"; }
			else
				{ echo "Error"; }

		}

}


# Let's test this connection class.
$connect = new DBcon();
$connect->DBcon('abcd','efghi');

?>

 

 

Thanks!

Link to comment
Share on other sites

You have a fatal syntax error on line 4.

 

You should be learning php (or learning something new in php), developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php will help you by displaying all the errors it detects. You will save a TON of time.

Link to comment
Share on other sites

To begin with, the problem you're having is on line 4, which is when you're defining a class.

 

A class isn't a function, when you define a class you don't need parentheses (the brackets). When you initiate a class, you should include them though.

 

You should also have some kind of error handling, using or die() is bad practice, especially so in OOP.

Link to comment
Share on other sites

LOL the () are a force of habbit from using too many functions in my lifetime.

While the page now works, it seems that $connect->DBcon('abcd','efghi'); doesnt actually give the values to the variables in the function. My syntax is correct no?

Link to comment
Share on other sites

Woot i got it. Thanks Guys!

<?php
# OOP LEARNING PAGE #
error_reporting(E_ALL);
ini_set("display_errors", 1);

class DBcon
{
	function DBcon($username,$password)
		{
			$db = mysql_connect("localhost", $username, $password); 

			if(mysql_select_db("tablename",$db))
				{ echo "Success"; }
			else
				{ echo "Error"; }
		}
}


# Let's test this connection class.
$connect = new DBcon('abcd,efghi');

?>

Link to comment
Share on other sites

You're using the same namespace for the function. The class is already named DBCon, and inside that class you also have a function named DBCon.

 

<?php
class MySQLConn {
  function __construct($username, $password, $database, $host = 'localhost') {
    $conn = mysql_connect($host, $username, $password);
    if ($conn) {
      if (mysql_select_db($database, $conn)) {
        echo "Success";
      }
      else {
        echo "Failed";
      }
    }
  }
}


// This will automatically connect to MySQL once the object has been initiated because of using a constructor
$db = new MySQLConn('username', 'password', 'database_name');
?>

 

http://www.php.net/manual/en/language.oop5.decon.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.