How to Define Global Functions in PHP

How to define global functions in PHP

In file include.php:

function myGlobalFunction() {
// Do something
}

Then in every page you want to use it:

include 'include.php';
myGlobalFunction();

PHP define global variables for functions?

I suggest that you store your array globally using the $GLOBALS, and instead of using echo to print an array, use the print_r method instead.

<?php
$GLOBALS["titleArray"] = array('world');

function foo(){
print_r($GLOBALS["titleArray"]);
}

function boo(){
array_push($GLOBALS["titleArray"], "Hello");

print_r($GLOBALS["titleArray"]);
}

foo();
boo();

how to create a PHP function global

You can't create global function without including the file where you defined them.
In the case of WP, the file may be included by a dependency but you can be sure it's included somewhere :)

Two way to include your file :

  1. Using php native function in all your files :
    http://www.php.net//manual/fr/function.include.php
  2. Using PSR standard : http://www.php-fig.org/psr/psr-0/

Use the PSR standard if you can.

Define global function from within PHP namespace

It's possible but aside from being bad design, you will have to enclose your code within brackets for each namespace and won't be able to use namespace my_module; Instead it will have to be namespace my_module { ... }.

Example:


namespace my_module {

function module_function()
{
// code
}
}

namespace {
// global namespace.

function my_module()
{
// call your namespaced code
}
}

How to declare a global variable in php?

The $GLOBALS array can be used instead:

$GLOBALS['a'] = 'localhost';

function body(){

echo $GLOBALS['a'];
}

From the Manual:

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.


If you have a set of functions that need some common variables, a class with properties may be a good choice instead of a global:

class MyTest
{
protected $a;

public function __construct($a)
{
$this->a = $a;
}

public function head()
{
echo $this->a;
}

public function footer()
{
echo $this->a;
}
}

$a = 'localhost';
$obj = new MyTest($a);

When is a global function not a callable?

The most reasonable explanation is that your code is not in global namespace. Like below

<?php
namespace App;

function php() {
}

var_dump(is_callable('App\\php'));

how to create global function that can be accessed from any controller and blade file

Updated:

Step 1
Add folder inside app folder
app->Helper

Step 2
add php Class inside Helper folder
Eg. Helper.php

Add namespace and class to the Helper.php

namespace App\Helper;

class Helper
{

}

Register this Helper.php into config/app.php file

'aliases' => [
....
'Helper' => App\Helper\Helper::class
]

Now, write all the functions inside Helper.php and it will be accessible everywhere.

How to access from Controller?

Step 1 - Add a namespace at top of the controller.

use App\Helper\Helper;

Step 2 - Call function - Assume there a getInformation() inside the Helper Class.

$information = Helper::getInformation()


Related Topics



Leave a reply



Submit