How to define global functions in PHP

前端 未结 7 1286
陌清茗
陌清茗 2020-12-01 11:43

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

相关标签:
7条回答
  • 2020-12-01 12:16

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

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

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

    Basic usage

    0 讨论(0)
  • 2020-12-01 12:24

    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_file and 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_path in php.ini to find the file.

    0 讨论(0)
  • 2020-12-01 12:25

    In file include.php:

    function myGlobalFunction() {
        // Do something
    }
    

    Then in every page you want to use it:

    include 'include.php';
    myGlobalFunction();
    
    0 讨论(0)
  • 2020-12-01 12:32

    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.

    Sandbox\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

    0 讨论(0)
  • 2020-12-01 12:34

    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
    
    0 讨论(0)
  • 2020-12-01 12:35

    Then in every page you want to use it:

    include 'include.php'; myGlobalFunction();

    -

    Put it in an include, then include it.

    This technically may 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
        function echo_1() {
           echo 1;
        }
    
        echo_1();
    
        require 'boot.php';
    

    boot.php

    <?php
        include_once 'page.php';
    
        echo_1();
        echo_9342949();
    

    page.php

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

    With that, your output would be 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.

    0 讨论(0)
提交回复
热议问题