0

I would like to use vaiables defined in another php file in a included file in a function.

I have something like this:

/* INDEX.PHP */
require('main.php');
require('utils.php');

$error = ([condition]);

if ($error) {
    error_404();
}

require('page.php'); //The normal content of the page, replaced by error404.php in case of error


/* UTILS.PHP */
function error_404() {
    header('HTTP/1.0 404 Not Found');
    include('error404.php'); //This file needs the 'main.php' required outside the function.
    exit();
}

The error404.php file included in the error_404() function needs to use the functions and variables declared in the main.php file included outside the function.

I need something like "move the include main.php to the function environment". Does anyone know how can I solve this problem?.

Timberman
  • 647
  • 8
  • 24
Joseph
  • 335
  • 1
  • 3
  • 13
  • Don't include the 404 page, redirect to the 404 page. – Timberman Dec 03 '19 at 14:23
  • Anything in `main.php` will be in the same scope as the top-level code in `index.php`. The same is true for `error404.php`, except that it is in the scope of the `error_404()` function. So there's really do difference between the code in those files and the code directly on the `index.php` page. What you do need to be mindful of though, is [variable scope](https://www.php.net/manual/en/language.variables.scope.php). But that would apply whether you were `include`ing files or not. – Patrick Q Dec 03 '19 at 14:24
  • @Timberman If I redirect, the client will receive a 200 code and the requested url will be lost... – Joseph Dec 03 '19 at 14:26
  • @Manuel23 Then put the variables in a session and use them in the error404.php – Timberman Dec 03 '19 at 14:27
  • 1
    @Manuel23 It would probably be helpful to see the code of `error404.php` and for you to try to describe in words what exactly it is you are ultimately trying to accomplish. It seems like you would probably benefit from implementing an object-oriented architecture. – Patrick Q Dec 03 '19 at 14:30
  • 1
    Have you considered autoloading classes rather than using global functions? That can replace most of the include/require stuff. – Don't Panic Dec 03 '19 at 14:30
  • @PatrickQ Yes, in the future I will migrate to OOP, but for now I have to solve it in this way since it would take me a long time to migrate it. The `error404.php` file includes the `header.php` file that generates the page header and it needs the functions and variables of `main.php` – Joseph Dec 03 '19 at 14:35
  • @Manuel23 Don't wait. Do it now. You'll thank yourself later. You're going to spend way more time trying to keep track of all these includes than you will implementing OOP. What you have now is a maintenance nightmare. – Patrick Q Dec 03 '19 at 14:38
  • 1
    Regardless, there's no reason the code you've shown here wouldn't work. I set up a folder with those exact files on my system and it does work. Can you be more specific about what's going wrong? Are you getting undefined variable or function errors? – Don't Panic Dec 03 '19 at 14:47
  • @Don'tPanic I am getting an 'undefined' error in the file 'error404.php' when I try to use a variable of the 'main.php' – Joseph Dec 03 '19 at 15:16
  • @PatrickQ Yes, I understand but I don't have much experience with OOP, it would take time to get it right. I have trouble creating a solid structure. Do you have any material to recommend me? Thank you! – Joseph Dec 03 '19 at 15:25
  • @Manuel23 The [PHP manual](https://www.php.net/manual/en/language.oop5.php) entry for OOP would probably be a good starting point, as well as the separate entry for [Classes/Objects](https://www.php.net/manual/en/book.classobj.php). Once you have a basic understanding, you may also want to look into using a framework such as Laravel, ZendFramework, Symfony, or CodeIgniter. These frameworks will provide you with a solid MVC structure, which will make what you're trying to do a lot easier. – Patrick Q Dec 03 '19 at 15:32
  • @Manuel23 I am _guessing_ the undefined error comes when you try to use a variable from main.php inside a function in error404.php. _If that's the case_, the problem isn't with your include/require, but is simply a variable scope problem. I'm not sure what you already know about that, but a function does not automatically have access to variables not defined within it or passed as arguments. – Don't Panic Dec 03 '19 at 15:33
  • @PatrickQ I have knowledge about OOP but I have trouble creating an elegant structure, I always end up getting tangled up. I will keep in mind what you tell me. Thank you! – Joseph Dec 03 '19 at 15:37
  • @Don'tPanic Yes, I know, that is exactly what I am trying to solve, I am looking for some way to deal with that problem, the variables declared in `main.php` are outside the scope of the `error_404()` function – Joseph Dec 03 '19 at 15:39
  • Does this answer your question? [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Patrick Q Dec 03 '19 at 15:44

0 Answers0