Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

__Construct() problem - php

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Watch

pixelbypixel

Established Member
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Which PHP version are you using?
 
0
Close
Host has 5.x.x

Think it defaults to php4, so i have used .php5 file ext and same problem occurs.
 
0
Close
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
Close
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
Close
I'd agree with Eric...BUT:
Try
Code:
$connection = new connection;

?
 
0
Close
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
Close
The problem has been solved.

Eric already spoke to me, i've updated php as my host had an old version installed.
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Search: choose where to search from the All tab
Open the search at the top of any page. On the All tab, the list next to the search box lets you choose where to search: everything, all...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back