What is a module in PHP?

9

I'm studying this documentation and I came across this 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.

    
asked by anonymous 28.02.2017 / 18:06

3 answers

9

In this context it was a way to generalize the place where the function was called or where the code is running, in this case the return , since it can be called from several places and it is possible to return from any part of the < in> script . You can:

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

Understand that a PHP script file looks like it's a function, so you can terminate execution at any point. If the code can return both from a function and a script , what name would you use to refer to both? They chose module.

An example can be seen in the Guilerme Birth response.

This starts to be explained below in the documentation:

  

If called in global scope, execution of the current script is terminated. If the current script file is included or required with the include or or require functions, the control is passed back to the script it is calling. In addition, if the current script was included with the include function, the value returned will be returned as the value of the include call. If a return is called from within the main script, its execution will be terminated. If the current script is mentioned in the auto_prepend_file or auto_append_file php.ini configuration options, the script execution will be terminated.

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

    
28.02.2017 / 18:13
8

I may be wrong, but choice of the term module is to refer to two situations where return can be used, for example I believe most will presume that return is used in functions, yes indeed is correct:

function foo() {
    return 1;
}
However, return is supported in PHP by include and require (possibly by the first call of _once , but this is another story, then I explain).

For example if you do this:

test.php:

<?php
return 'teste';

And call it like this:

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

You'll get this:

  

string (5) "test"

So I think they chose the module name for this reason, since function is one thing and include is another, internally (at the lowest level) may even be "identical", but at PHP level they are different things, then it's like 2 modules really, a script and another include .

    
28.02.2017 / 18:54
3

Module is the calling script

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

I believe it was written this way because return has an additional use beyond the common use of returning the value when it is inside 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 include command to return the settings of that file.

For example:

config.php

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

search.php

<?php

$config = (require 'config.php');
    
28.02.2017 / 19:43