0

I created a config file that has a variable I want to use as a constant.

<?php
$ROOT_PATH = 'C:/Users/me/Documents/app-qas.com/site';
?>

On a CLASS page where I want to use the variable I added the following before instantiating the CLASS:

include_once("config.php");
$root=$ROOT_PATH;

I made the appropriate scoping changes to the functions in my class as follows: global $root; include_once($root."/Library/API/database.inc.php");

When I run my app, it DOES perform all of the data connections it is designed to do, BUT it STILL returns the following errors:

Warning: include_once(config.php): failed to open stream: No such file or directory in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 5

Warning: include_once(): Failed opening 'config.php' for inclusion (include_path='.;C:\php\pear') in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 5

Notice: Undefined variable: ROOT_PATH in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 6

Warning: include_once(/Library/API/database.inc.php): failed to open stream: No such file or directory in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 112

Warning: include_once(): Failed opening '/Library/API/database.inc.php' for inclusion (include_path='.;C:\php\pear') in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 112

Warning: include_once(/Library/API/database.inc.php): failed to open stream: No such file or directory in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 112

Warning: include_once(): Failed opening '/Library/API/database.inc.php' for inclusion (include_path='.;C:\php\pear') in C:/Users/me/Documents/app-qas.com/site'\class\Posting.class.php on line 112

If I comment out the include and hard code the $root it runs like before BUT it DOES NOT throw any errors:

#include_once("joblaunch.php");
#$root=$ROOT_PATH;
$root='C:/Users/me/Documents/app-qas.com/site';

I don't understand why it runs and throws errors when getting the variable from the config.php but runs and doesn't throw an error when hard coding the path.

10
  • Why don't you just use define('ROOT_PATH', 'C:/Users/me/Documents/app-qas.com/site'); and use ROOT_PATH. Then you won't have any scoping problems. Commented Nov 10, 2016 at 21:15
  • Also, you need to put global $root inside the functions that use it. Not in the global scope. Commented Nov 10, 2016 at 21:16
  • So for example: if you have functions in your database.inc.php file, then the global $root has to go inside those functions. Not before you include the file. Commented Nov 10, 2016 at 21:18
  • @KodosJohnson I tried this (I already had the global $root in the functions). See my comment below. Commented Nov 10, 2016 at 21:23
  • It looks like your script isn't even including the config.php file. The first error says it didn't find it. That's why it's not recognizing your config variable. Are you sure config.php is in the same directory? Commented Nov 10, 2016 at 21:24

1 Answer 1

0

if you define a constant instead of a variable, this problem should be solved,

definition

old:

$ROOT_PATH = 'C:/Users/me/Documents/app-qas.com/site';

new:

define('ROOT_PATH', 'C:/Users/me/Documents/app-qas.com/site');

usage

old:

$root=$ROOT_PATH;

new:

$root = ROOT_PATH;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried what you suggested and the same thing occurs. I changed the config.php to the define statement. I changed the other file to $root = ROOT_PATH;, The error message is the same except the NOTICE which is now: Notice: Use of undefined constant ROOT_PATH - assumed 'ROOT_PATH'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.