Php Syntax error question
Hi i have the following directory structure:
Main Folder -> Classes -> user_classes
all nested in each other. I have the Following files inside Classes directory
always_include_top.php
custom_error_handler.php
config.php
database.php
Out of which the database.php file is as follows:
<?php
include_once("always_include_top.php");
include_once("config.php");
include_once("custom_error_handler.php");
include_once ("user_classes/newDatabase.php");
class Database extends newDatabase
{
// some more code... with extra functions
public function dbBackUp($backupfile = NULL)
{
//code...
}
}
?>
I have the Following file at User user_classes directory
newDatabase.php
The code sample for this file is
<?php
include_once("../always_include_top.php");
include_once("../config.php");
include_once("../custom_error_handler.php");
error_reporting(E_ALL);
class newDatabase
{
// my code goes here
}
?>
Why do i get the following error in classes/database.php ( No error in classes/user_classes/newDatabase.php )
Warning: include_once(../always_include_top.php) [function.include-once]: failed to open stream: No such file or directory in E:wampwwwgreeting_cardsadmclassesuser_classesdatabase.php on line 2
Warning: include_once() [function.include]: Failed opening '../always_include_top.php' for inclusion (include_path='.;C:phppear') in E:wampwwwgreeting_cardsadmclassesuser_classesdatabase.php on line 2
Warning: include_once(../config.php) [function.include-once]: failed to open stream: No such file or directory in E:wampwwwgreeting_cardsadmclassesuser_classesdatabase.php on line 4
Warning: include_once() [function.include]: Failed opening '../config.php' for inclusion (include_path='.;C:phppear') in E:wampwwwgreeting_cardsadmclassesuser_classesdatabase.php on line 4
Warning: include_once(../custom_error_handler.php) [function.include-once]: failed to open stream: No such file or directory in E:wampwwwgreeting_cardsadmclassesuser_classesdatabase.php on line 5
Warning: include_once() [function.include]: Failed opening '../custom_error_handler.php' for inclusion (include_path='.;C:phppear') in E:wampwwwgreeting_cardsadmclassesuser_classesdatabase.php on line 5
Fatal error: Cannot redeclare class Database in E:wampwwwgreeting_cardsadmclassesdatabase.php on line 12
I want both files to individually compile. As i would be including the files in other files based on page types. Whats the problem with including here?
When you use include_once()
, it doesn't change to the directory where a particular file is located and execute it there, it executes the contents of that file within the context of the current file. So, classes/user_classes/database.php is executed as if it were in the classes folder. ".." refers to the Main folder in that case, so it's looking for the first three files in the Main folder. Those files aren't in the Main folder, so it gives you the warnings.
上一篇: 路径在php.ini中
下一篇: Php语法错误问题