php PHP中如何定义全局函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4458837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:05:53  来源:igfitidea点击:

How to define global functions in PHP

phpfunctionglobal

提问by Sussagittikasusa

How can I define a global function which would be accessible from any page?

如何定义可从任何页面访问的全局函数?

回答by Jordi

In file include.php:

在文件include.php 中

function myGlobalFunction() {
    // Do something
}

Then in every page you want to use it:

然后在您要使用它的每个页面中:

include 'include.php';
myGlobalFunction();

回答by user2656037

If you want your function to always be available, without including it, do this:

如果您希望您的功能始终可用,而不包含它,请执行以下操作:

  1. Create your function in a PHP file.

  2. In your php.ini file, search for the option auto_prepend_fileand add your PHP file to that line, like this:

    `auto_prepend_file = "/path/to/my_superglobal_function.php"`
    

    Or if you write it with a non absolute path, like this:

    auto_prepend_file = "my_superglobal_function.php"

    It will look in your include_pathin php.inito find the file.

  1. 在 PHP 文件中创建您的函数。

  2. 在您的 php.ini 文件中,搜索该选项auto_prepend_file并将您的 PHP 文件添加到该行,如下所示:

    `auto_prepend_file = "/path/to/my_superglobal_function.php"`
    

    或者,如果您使用非绝对路径编写它,如下所示:

    auto_prepend_file = "my_superglobal_function.php"

    它会在您的目录include_pathphp.ini查找文件。

回答by luarwo

You could declare a function inside a function. Be careful to call the outside function only once or you'll get an error.

您可以在函数内声明一个函数。注意只调用一次外部函数,否则会出错。

class MyClass {

  function declareGlobalsFn () {
    // Functions declared inside a function have global scope

    function globalfn1() {echo "fn1";}

    function globalfn2() {echo "fn2";}
  }
}

$ob = new MyClass();
$ob->declareGlobalsFn();

globalfn1(); // fn1
globalfn2(); // fn2

回答by Super Cat

Then in every page you want to use it:

include 'include.php'; myGlobalFunction();

然后在您要使用它的每个页面中:

include 'include.php'; myGlobalFunction();

-

——

Put it in an include, then include it.

把它放在一个包含中,然后包含它。

This technicallymay not be correct, depending on the context.

这在技术上可能不正确,具体取决于上下文。

'Page' could be perceived as 'file'. For example, "You must include the function's file within each file you want to use the function".

“页面”可以被视为“文件”。例如,“您必须在要使用该函数的每个文件中包含该函数的文件”。

Once a function is defined in your program, it can be accessed from anywhere moving forward up until the program has finished executing.

一旦在程序中定义了一个函数,就可以从任何地方访问它,直到程序完成执行。

Say you have this:

说你有这个:

index.php:

索引.php:

<?php
    function echo_1() {
       echo 1;
    }

    echo_1();

    require 'boot.php';


boot.php

启动文件

<?php
    include_once 'page.php';

    echo_1();
    echo_9342949();


page.php

页面.php

<?php
    function echo_9342949() {
      echo 9342949;
    }

    echo_1();


With that, your output would be 1119342949.

这样,您的输出将是1119342949.

Of course, when you say 'page' you may literally mean a directly accessed stand-alone 'page file', in which case the answers from the other users will suffice. However, if you're looking to use the same function from different locations throughout your program, simply define it before you intend to use it and you can access it anywhere moving forward regardless of scope.

当然,当您说“页面”时,您的字面意思可能是直接访问的独立“页面文件”,在这种情况下,其他用户的回答就足够了。但是,如果您希望在整个程序的不同位置使用相同的函数,只需在打算使用它之前定义它,并且无论范围如何,您都可以在任何地方访问它。

However, if you're looking to use the same function from different locations throughout your program, simply define it before you intend to use it and you can access it anywhere moving forward regardless of scope.

但是,如果您希望在整个程序的不同位置使用相同的函数,只需在打算使用它之前定义它,并且无论范围如何,您都可以在任何地方访问它。

This of course isn't true for things like class functions, but for normal functions this remains true.

这当然不适用于类函数之类的东西,但对于普通函数来说,这仍然是正确的。

回答by jimconte

To expand on luarwo's answer, you can declare the function right in your class constructor. This could make your class a sort of function library, where the functions are accessible from any page where you create your class instance.

要扩展luarwo 的答案,您可以在类构造函数中声明该函数。这可以使您的类成为一种函数库,可以从您创建类实例的任何页面访问这些函数。

Sandbox\MyGameLib

沙盒\MyGameLib

<?php
namespace Sandbox;
class MyGameLib {
    public function __construct() {
        if (!function_exists('loveGame')) {
            function loveGame($game) {
                print "The game $game is awesome";
            }
        }
    }
}

Seesaw

跷跷板

<?php
namespace Seesaw;
use Sandbox\MyGameLib;

$m = new MyGameLib();
loveGame('Tempest');

The game Tempest is awesome

游戏暴风雨真棒

回答by Robert

Put it in an include, and then include it.

把它放在一个包含中,然后包含它。

回答by Pankaj Agrawal

I would suggest PHP Composerwith autoloading. You can take a look at the laravel implementation for a helper function.

我建议使用自动加载的PHP Composer。您可以查看辅助函数的 Laravel 实现。

In brief, Just define your helper function script in the autoloading section and the PHP composer will take care of it.

简而言之,只需在自动加载部分定义您的辅助函数脚本,PHP 编写器就会处理它。

Note: Do not forget to include the autoload.php file at the top level of your project.

注意:不要忘记在项目的顶层包含 autoload.php 文件。

Basic usage

基本用法