NameSilo

__Construct() problem - php

Spaceship Spaceship
Watch
Impact
38
Hey,

Been a year since I did OO php. Last time I used this I remember it working fine.. but..

My index.php
PHP:
<?php 

include("includes/dbcon.php");

$connection = new connection();

?>

....

My dbcon.php
PHP:
<?php

class connection {


	 /*	Main function 
		Connects to database
		Die with error if fails to connect */

	function __construct(){
		/* 	Database Connection Varaiables */
		$usrnme = "user";
		$psswrd = "pass";
		$servhst = "local";
		$dbnme = "db";

		$con = mysql_connect($servhst, $usrnme, $psswrd);
		if (!$con)
		 {
			 die('Failed to connect: ' . mysql_error());
		 }

		mysql_select_db($dbnme, $con);

	}

}

?>

Now, last I checked, php is meant to automatically run __construct() when a new class is created. I created a new class of connection in the index file, but it does not run the __construct()

However, it works if I add: $connection->__construct(); to the index.php

Has something changed regarding this? Or am I being blind and missed something completely? O_o
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Which PHP version are you using?
 
0
•••
Host has 5.x.x

Think it defaults to php4, so i have used .php5 file ext and same problem occurs.
 
0
•••
I don't think the server is parsing PHP properly. PHP 5 *will* automatically run __construct.

.htaccess
Code:
AddType application/x-httpd-php .php .php5

EDIT: however, taking another look - the extension of your include file is .php which you say may default to PHP4 - try changing it to PHP5 as well?
 
Last edited:
1
•••
Still not working.

Thanks though.

I'll get in touch with my host, see if they will change default php to 5 instead of 4.


RE: your edit; I did edit the file ext in all combinations :p
 
Last edited:
0
•••
I'd agree with Eric...BUT:
Try
Code:
$connection = new connection;

?
 
0
•••
Use:

PHP:
echo 'PHP Version: ' . phpversion()."\n";

in your script to check whether you are actually running under PHP5.

The extension of the include files shouldn't matter (they don't even have to end with ".php").
 
0
•••
The problem has been solved.

Eric already spoke to me, i've updated php as my host had an old version installed.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back