2

I'm just trying to run a website on my local which I copied from server. In my config.php:

define ( 'DB_SERVER', '127.0.0.1' );
define ( 'DB_USERNAME', 'user' );
define ( 'DB_PASSWORD', 'password' );
define ( 'DB_DATABASE', 'database' );

class DB_Class {
    function __construct() {
        $connection = mysql_connect ( DB_SERVER, DB_USERNAME, DB_PASSWORD ) or die ( 'Connection error -> ' . mysql_error () );
        mysql_select_db ( DB_DATABASE, $connection ) or die ( 'Database error -> ' . mysql_error () );
    }
}

In my user.php:

include_once 'config.php';

class User {
    // Database connect
    public function __construct() {
        $db = new DB_Class ();
    }
}

In my index.php:

session_start ();
include_once ('classes/user.php');
$uobj = new User ();

Now whenever I attempt to create an object of a class User it returns a Fatal error: Class 'DB_Class' not found on the line where I attempt, in this case it's line 6. Though it works perfectly fine on server.

I tried to extend the class User to the class DB_Class but then again the same error on the line where I attempt to extend.

Someone please point out what's wrong. Thanks.

Mehulkumar
  • 836
  • 2
  • 10
  • 23
  • do you include the user.php wherever you instantiate the User object? (outside user.php file) – Dinesh Dec 11 '14 at 17:21
  • Yes indeed and my guess is User object creates successfully but I think problem is somewhere in user.php where it creates DB_class object. Maybe some permission error. Sorry I don't know much about it. – Mehulkumar Dec 11 '14 at 17:31
  • try $db = new \DB_Class (); – Aris Dec 11 '14 at 17:43
  • What happens if you use `require_once` rather than `include_once`? – Matt Gibson Dec 11 '14 at 17:46
  • @Aris I tried but same error. – Mehulkumar Dec 11 '14 at 17:46
  • Is your local system a Linux ? – Alexis Peters Dec 11 '14 at 17:48
  • @mattgibson I tried that too. The same. – Mehulkumar Dec 11 '14 at 17:48
  • Do a command line lint check `php -l ` on your config.php. – Mark Baker Dec 11 '14 at 17:49
  • Well, I've just pasted your exact code into two files with those names, and it works fine, with no compilation errors. What's in the code you're not showing us? – Matt Gibson Dec 11 '14 at 17:51
  • I did the same as @MattGibson and the code works fine.. can you show the code where you instantiate the User class.. – Dinesh Dec 11 '14 at 17:53
  • @mattgibson I've added the code where I create an object of a class 'User' – Mehulkumar Dec 11 '14 at 18:02
  • I'm pretty sure that `include_once 'config.php';` in your user.php file will use the path relative to your index.php file. It's been a while, but I generally use full paths when it comes to includes within includes. http://stackoverflow.com/questions/3334046/calling-include-from-an-included-file – Robbert Dec 11 '14 at 18:05
  • 1
    @Robbert Include problems were why I suggested using `require_once` instead for diagnosis—a `require_once` on a file that can't be found is a fatal error, so it should have changed the result. – Matt Gibson Dec 11 '14 at 18:11
  • @AlexisPeters No I'm running in XAMPP environment on windows. – Mehulkumar Dec 11 '14 at 18:13
  • @Robbert I tried using full URL and the answer I got for the error I got is this http://stackoverflow.com/questions/23285503/warning-require-once-http-wrapper-is-disabled-in-the-server-configuration. Now I'm confused which one to use. – Mehulkumar Dec 11 '14 at 18:19

1 Answers1

2

It appears that your site structure is set up with the two class files in a folder called classes. As this link suggests, relative paths in includes rarely go well because it's the calling script's path that is used for all includes, including those within other include files. Unless you have specified the classes folder as in your path, index.php won't know where the config.php file is. You should do this in your user.php file.

include_once $_SERVER['DOCUMENT_ROOT'] . '/classes/config.php';

Or use

require_once dirname(__FILE__) . '/config.php';

Either one will remove the relative link and allow the file to be included anywhere within your directory structure.

Community
  • 1
  • 1
Robbert
  • 6,481
  • 5
  • 35
  • 61
  • 1
    One thing I'm still confused about that if php couldn't find my file in my relative path, why it wasn't giving any error? Any idea? – Mehulkumar Dec 11 '14 at 18:28
  • Here also you may use [directory_separator constant](http://php.net/manual/en/dir.constants.php) to make your application compatible with both windows and linux – SaidbakR Dec 11 '14 at 18:28
  • 1
    include throws only warnings so if don't have that level of error reporting enabled, you won't see it. Require will throw a fatal error, however. – Robbert Dec 11 '14 at 18:48