I'm trying to switch from procedural to OOP php, I can connect to a database and use it fine in procedural however after a few hours of head scratching, google searches and little tweaks/changes I concede defeat and conclude I must be missing something.
I have two files, my IDE marks that the syntax issue is apparently with the "function"'s part of the first file (test.php) but I just can't figure out what, does any body have any ideas? the two files are below. Sorry for the formatting but for some reason I can't tab within this textarea.
I'm assuming its something to do with a lack of constructor. I've tried to add one in instead of the config function but I still have syntax issues. In documentation/tutorials I can find mention of constructors in the php and functions in the html but nothing seems to explain how they work together with an example.
Disclaimer: both files are surrounded by <?php ?>
tags and I am not addressing security measures currently. Walk before you can run and all that. I've also tried to throw a doctype at the beginning of test.php and pad it with <html>
<head>
and <body>
tags where appropriate but to no avail. Using Apache 2.2.22 and PHP 5.3.13.
// ------- Test.php start -------
include 'obj_lib.php';
$db = new database;
$db = function config('test', 'root', '', 'localhost');
$db = function connect();
$db = function test_database();
// ------- Test.php end -------
// ------- obj_lib.php start -------
class database
{
private $username;
private $password;
private $database;
private $server;
private $db_handle;
private $db_found;
public function config($indatabase=null, $inusername=null, $inpassword=null, $inserver=null)
{
$this->username = $inusername;
$this->password = $inpassword;
$this->database = $indatabase;
$this->server = $inserver;
}
public function connect()
{
$this->db_handle = mysql_connect($this->server, $this->username, $this->password);
$this->db_found = mysql_select_db($this->database, $this->db_handle);
}
public function test_database()
{
echo "$this->username"."$this->password"."$this->database"."$this->server"."$this->db_handle"."$this->db_found";
}
}
// ------- obj_lib.php end -------