What is a module in PHP?

Asked

Viewed 302 times

9

I’m studying this one documentation and I came across that term :

The Return statement returns control of the program to the module that called it. The execution will continue in the expression following the invocation of the module.

Hence the doubt.

3 answers

9

In this context it was a way for them to generalize where the function was called or where the code is being executed, in the case of the return, since it can be called from several places and it is possible to return from any part of the script. Can:

  • be called another function,
  • call to result in a member of a class,
  • call right into the script.

Understand that a file of script PHP is like a function, so it is possible to terminate the execution at any point. If the code can return both of a function how much of a script, what name would you use to refer to both? They chose module.

An example can be seen in the answer of the Birth Guilerme.

This begins to be clarified just below in the documentation:

If called in the global scope, the current script execution is completed. If the current script file is included or required with include or or require functions, the control is passed back to the script you are calling. Also, if the current script was included with the include function, the value reported to Return will be returned as the value of the include call. If a Return is called from within the main script, its execution will be completed. If the current script is mentioned in the auto_prepend_file or auto_append_file php.ini configuration options, the execution of the script will end.

Obviously a function is called in a script from another file of script has some constant implications in the documentation and there are examples down the page. He has to change this context since in PHP the application is not a single thing as it is in compiled languages.

8

I may be mistaken, but choosing the term module is to refer to two situations where the return can be used, for example I believe that the majority will assume that the return is used in functions, yes indeed it is correct:

function foo() {
    return 1;
}

However the return is supported in PHP by include and require (eventually by the first call of _once, but this is another story, then I explain).

For example if you do this:

php test.:

<?php
return 'teste';

And call it that:

<?php
$x = include 'teste.php';
var_dump($x);

Will get this:

string(5) "test"

So I guess they chose the module name for this reason, since function is a thing and include is another, internally (at the lowest level) may even be "identical", but at the level of PHP are different things, so it is as if it were really 2 modules, a script and another include.

3

Module is the calling script

The expression "module" in this context has meaning similar to "another script that is not the PHP file itself".

I believe it was written that way because the return has an additional use in addition to the common use of returning the value when it is within a function, which is the same behavior as other programming languages.

In this other use of return, it is common to create a PHP file, for example, only with settings and use the command include to return the settings of that file.

For example:


config.php

<?php
return [
  'usuario' => 'user123',
  'banco' => 'adm',
  'servidor' => 'locahost'
];

php search.

<?php

$config = (require 'config.php');

Browser other questions tagged

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