I
I
IsaevDev2015-08-31 03:24:54
PHP
IsaevDev, 2015-08-31 03:24:54

How does include/require actually work in PHP?

How does everything really happen? What are the scopes? Does it depend on the nesting of files in the system? Do scopes depend on "nesting" include/require? Does include/require affect .htaccess
Let's say I include Config.php from the index page, which is located in ./core
Config.php includes files from core
Then I include modules ./customer/index.php or /admin/index from the index page .php
And I'm constantly faced with the unavailability of any variables, methods and even classes
. And no logic can be traced
. How does it all really work? How to connect modules correctly?
UPD 1
File structure:
/root
---index.php
---action.php
---/core
------config.php
------core.php
------utils.php
---/customer
------index.php
------action.php
core/config.php

<?
require_once "core.php";
require_once "utils.php";
?>

core/core.php
<?
require_once "utils.php";
?>

core/utils.php
<?
function prepare_db_val()
{
...
}
?>

index.php
<?
require_once "core/config.php";
if( isset($_GET['act']))
{
   require_once "action.php"; // не работает
   require_once "customer/index.php"; // работает!
}
?>

customer/index.php
<?
if( isset($_GET['act']))
{
   require_once "action.php";
}
?>

action.php, costomer/action.php
<?
$db_q = prepare_db_val($_GET['val']);
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
index0h, 2015-08-31
@IsaevDev

How does everything really happen?

Imagine that instead of include/require statements there will be your code))
Global by default. For the rest - use, or namespace prefix.
Not
Not
Not
It's called autoloading, discover composer and don't reinvent the wheel.
We read on the topic PSR-4
Z.Y. Don't use include/require, from Total, require_once instead.

V
Vitaliy K, 2019-08-20
@revenger

5d5bfdba6674b625482178.png
include or require by examples

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question