I have a function getHeader()
in the file include.php
.
Is it possible to call this respective function in my index.php
file without having to give include
, similar to Wordpress?
I have a function getHeader()
in the file include.php
.
Is it possible to call this respective function in my index.php
file without having to give include
, similar to Wordpress?
This is not possible due to the symbol table.
The symbol table defines the context in programming languages. Variables, functions, classes, objects, constants, etc. will have a reference stored in the symbol table when defined and, within a context, it is only possible to use the referenced items in the table.
Did not it make sense? Let's look at an example.
$x = "Variável X";
$y = "Variável Y";
function myFunction ()
{
return "Função myFunction";
}
In the index.php
file we define two variables and one function. We can check the symbol table for variables using the get_defined_vars
function:
print_r(get_defined_vars());
The return will be something like:
Array
(
[_GET] => Array
(
)
[_POST] => Array
(
)
[_COOKIE] => Array
(
)
[_FILES] => Array
(
)
...
[x] => Variável X
[y] => Variável Y
)
Note that all PHP global scope variables are also returned, because they belong to the global scope, they will be present in all the tables of symbols, regardless of context. For didactic purposes, we will remove the global variables, to display only the variables we declare:
$_globals = get_defined_vars();
$x = "Variável X";
$y = "Variável Y";
...
print_r(array_diff(get_defined_vars(), $_globals));
In this case, the output will only be:
Array
(
[x] => Variável X
[y] => Variável Y
)
That is, the variables $x
and $y
belong to the current context and, therefore, are defined in the symbol table. Now, if we change the context, for example, by calling a function and checking the symbol table of the new context:
$_globals = get_defined_vars();
$x = "Variável X";
$y = "Variável Y";
function myFunction ()
{
global $_globals;
print_r(array_diff(get_defined_vars(), $_globals));
return "Função myFunction";
}
The return will be an empty list. This is because the variables are no longer part of the context.
Array
(
)
If we define an internal variable for the function:
function myFunction ()
{
global $_globals;
$z = "Variável Z";
print_r(array_diff(get_defined_vars(), $_globals));
return "Função myFunction";
}
The result will be:
Array
(
[z] => Variável Z
)
Because $z
is the only variable in the current context. Exiting the function, when we check the symbol table again, we will have the same result as in the beginning, with the variables $x
and $y
, because we return to the same context. The variable $z
ceases to exist because it was a local variable to the function and will only exist within its context.
If we do the same analysis, but now checking the functions in the context, using the function get_defined_functions
, we'll look at the context of the file:
$x = "Variável X";
$y = "Variável Y";
function myFunction ()
{
return "Função myFunction";
}
print_r(get_defined_functions());
We have the output:
Array
(
[internal] => Array
(
[0] => zend_version
[1] => func_num_args
...
[1074] => xmlwriter_output_memory
[1075] => xmlwriter_flush
[1076] => dl
[1077] => cli_set_process_title
[1078] => cli_get_process_title
)
[user] => Array
(
[0] => handle_fatal
[1] => output
[2] => error_handler
[3] => myFunction
)
)
What interests us is the final part, identified by the index user
, where we find the function myFunction
, which we define in the program. If we now check the symbol table for functions within the function:
$x = "Variável X";
$y = "Variável Y";
function myFunction ()
{
print_r(get_defined_functions());
return "Função myFunction";
}
The result will be exactly the same. This is due to the fact that in PHP the functions belong to the global scope, existing in any part of the program, once it is defined.
But why, then, I can not call a function defined in another file without doing the include of the same?
Because if there is no include to the file where the function is defined, it will not be executed by PHP and the function will never be defined in the symbol table.
But I'm using Wordpress and I do not need to give include in files to call the functions. Why?
This is due to the fact that Wordpress does the "dirty work" for you. All the programming you do is in one of the files of the tool theme: page.php
, single.php
, etc. These files do not handle the HTTP request directly. Who does this is the actual index.php
file of Wordpress and in this file (or some other, following the line of execution), the theme files are included. In this case, the context you are working in the theme files there are functions that are defined in another file, but because Wordpress made them exist.
Nothing happens for magic.
In order to use functions from another php file you will have to include it, either using include, require or whatever it is, just like wordpress does, you may not know is where ...
The wp-includes / rest-api / class-wp-rest-request.php file has the function created there, and wp-settings.php does the require of this same file in
require( ABSPATH . WPINC . '/rest-api/class-wp-rest-request.php' );
in line 226 ...
As it turns out, it is not possible to do what you want
There's no way you can not include the other file, but there's a way to hide it, so you do not need to always add include(...)
to your code.
An example is to use auto_prepend_file
in PHP.ini:
auto_prepend_file=C:\caminho\para\funcao.php
This will make a require
of the file defined ( ie not "without giving include" ). However, your code will be without any type of include(...)
, for example:
function.php
function teste(){
return 'Isso é um teste';
}
index.php
<?php
echo teste();
Once set to auto_prepend_file=C:\caminho\para\funcao.php
, C:\caminho\para\funcao.php
will be included implicitly on all pages, this will make the index.php
work. ;)
Always set absolute paths, do not set as
auto_prepend_file=funcao.php
, as this will includefuncao.php
of the same folder. Therefore, accessing/www/index.php
will include/www/funcao.php
, but accessing/www/pasta/index.php
will include/www/pasta/funcao.php
.