Use external file function without giving include

Asked

Viewed 2,105 times

0

I have a function getHeader() in the archive include.php.

It is possible to call this function in my file index.php without having to give a include, similar to Wordpress?

  • I’m not sure I understand. You want to call a function that is in a file without ever including/loading that file?

3 answers

3

Not possible due to symbol table.

Symbol table

The symbol table defines the context in the 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 items referenced in the table.

Context of variables

Didn’t make sense? Let’s look at an example.

$x = "Variável X";
$y = "Variável Y";

function myFunction ()
{
  return "Função myFunction";
}

In the archive index.php we define two variables and a function. We can verify the table of symbols referring to the variables using the function get_defined_vars:

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, belonging to the global scope, they will be present in all symbol tables, regardless of context. For didactic purposes, we will remove the global variables in order 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 exit will be:

Array
(
    [x] => Variável X
    [y] => Variável Y
)

That is, the variables $x and $y belong to the current context and are therefore 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 a variable internal to 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
)

For $z is the only variable existing in the current context. Leaving the function, when checking again the table of symbols, we will have the same result as at the beginning, with the variables $x and $y, as we return to the same context. The variable $z ceased to exist for it was a local variable to the function and will exist only within the context of it.

Function context

If we do the same analysis, but now checking the existing functions in the context, through the function get_defined_functions, we will see to the context of the archive:

$x = "Variável X";
$y = "Variável Y";

function myFunction ()
{
  return "Função myFunction";
}

print_r(get_defined_functions());

We have the exit:

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 the function is myFunction, that we define in the program. If we now check the symbol table for the 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.

Answers

But why, then, I can’t call a function defined in another file without doing the include of it?

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 am using Wordpress and do not need to give include in files to call the functions. Why?

This is given to the fact that Wordpress does the "dirty work" for you. All the programming you do is in one of the theme files of the tool: page.php, single.php, etc. These files do not handle the HTTP request directly. Who does this is the file itself index.php of Wordpress and, in this file (or in some other, following the line of execution), the theme files are included. In this case, the context you are working on in the theme files exist functions that are set in another file, but because Wordpress made them exist.

Nothing happens by magic.

2

To be able to use functions from another php file you have to include it, either using include, require or whatever, just like wordpress does, you may not know where...

The file wp-includes/Rest-api/class-wp-Rest-request.php, has the function created there and the wp-Settings.php requires that same file in require( ABSPATH . WPINC . '/rest-api/class-wp-rest-request.php' ); on line 226... It’s not possible to do what you want

Link to wordpress code on Github

  • got it, so since I’m not an expert in php, I really don’t think that in wordpress I thought that he would find a way to do a get without include, but as he explained it implicitly in some file he makes this configuration, that’s it?

  • yes, this way or another, these "Imports" are made

  • In my view, what is best is the use of namespaces

1

No, but you can hide.

There is no way not to include the other file, however there is how to hide it, so not requiring adding the include(...) always in your code.

An example is using the auto_prepend_file in PHP.ini:

auto_prepend_file=C:\caminho\para\funcao.php

That will make a require of the defined file (that is, it is not "without giving include"). However, your code will be without any kind of include(...), for example:

php function.

function teste(){
    return 'Isso é um teste';
}

index php.

<?php

echo teste();

Once defined the auto_prepend_file=C:\caminho\para\funcao.php always the C:\caminho\para\funcao.php will be included implicitly on every page, this will make the index.php. ;)


Always set absolute paths, do not set as auto_prepend_file=funcao.php, because that will include the funcao.php of the same folder. Therefore, access the /www/index.php will include the /www/funcao.php, but access the /www/pasta/index.php will include the /www/pasta/funcao.php.

  • ah ta! Default type before only for later use, in the case of linux, then it would be the way /var/www/system/include ?

  • Yes, in linux you must also specify all the file path, /var/www/sistema/include/arquivo.php.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.