Erdős András
Erdős András

Reputation: 63

Why doesn't the "include" work in the example code?

The direction structure the following

When I trying to include the DBLink.php class to the CalendarClass.php, I get the following error messages:

Warning: include(../Database/DBLink.php): failed to open stream: No such file or directory in D:\apache\htdocs\Engine\Classes\CalendarClass.php on line 3

Warning: include(): Failed opening '../Database/DBLink.php' for inclusion (include_path='D:\apache\php\PEAR') in D:\apache\htdocs\Engine\Classes\CalendarClass.php on line 3

Fatal error: Class 'DBLink' not found in D:\apache\htdocs\Engine\Classes\CalendarClass.php on line 10

The content of the CalendarClass.php:

<?php

include "../Database/DBLink.php";

class CalendarClass{

    private $link;

     function __construct() {
        $this->link = new DBLink();
    }

    function UpdateCalendar(){
        $this->link->ClearCalendar();
    }

}

?>

What is the right way to include the DbLink.php to the CalendarClass.php ?

Upvotes: 0

Views: 49

Answers (2)

Erdős Andr&#225;s
Erdős Andr&#225;s

Reputation: 63

Solution: Change the include to include __DIR__ . "/../Database/DBLink.php"

Upvotes: 0

KhorneHoly
KhorneHoly

Reputation: 4766

You need to use

__DIR__ . "/../Database/DBLink.php";

PHP is looking for this file, but starts at a different root path.

You need to provide a full path so that PHP will look up the correct path.

__DIR__ provides the path to the directoy the file lies in.

An easier way would be to use autoloading

Upvotes: 2

Related Questions