Luminova Framework

PHP Luminova: Custom Global Functions & Constants

Last updated: 2026-01-12 17:23:11

Custom global functions let developers define reusable helper functions to extend application behavior and reduce repeated code.

Custom Global Functions are procedural helper functions and constants defined by the developer.They are autoloaded at runtime and can be used anywhere in your application.

These helpers are useful for small, reusable logic that doesn’t belong to a class.


Enabling Custom Global Functions

For safety, custom global functions are disabled by default.

To enable them, add the following to your .env file:

feature.app.dev.functions = enable

Once enabled, define your functions and constants inside:

/app/Utils/Global.php

Note:This file is automatically loaded by the framework.The filename or location cannot be changed (strictly Global.php).


Here’s a cleaned-up, beginner-friendly, and corrected version of that section. Straight talk, no fluff.


Global Check

You can check whether Developer Custom Global Functions were loaded by testing the __INIT_DEV_FUNCTIONS__ constant.

if (defined('__INIT_DEV_FUNCTIONS__')) {
    echo 'Custom global functions are loaded';
}

If the constant is not defined

Something is wrong. Check the basics first:

  1. Make sure feature.app.dev.functions is enabled in your .env file.
  2. Confirm /app/Utils/Global.php file exists and is in the correct location:
  3. Check the error logs for syntax errors or duplicate function/constant definitions.

Tip:

Always guard critical logic with defined() checks if your application depends on them.

Alternatively, use Luminova\Funcs\func() to call your function. It performs proper checks and will throw a runtime error ifthe function does not exist or is disabled—fail fast, fail loud, no silent bugs.


Important Guidelines

To avoid conflicts and unexpected behavior:

  1. Always check if a function or constant already exists before defining it.
  2. Prefer prefixes or namespaces to reduce naming collisions.
  3. Keep global helpers small and simple — avoid complex logic.

Global scope is powerful. Use it carefully.


Examples

Simple Global Helpers

You can use Luminova\Funcs\function_exists_cache instead of function_exists to avoid repeated existence checks.

With Safety Checks:

// /app/Utils/Global.php

// Define a constant safely
defined('MY_FOO') || define('MY_FOO', 'bar');

// Define a function safely
if (!function_exists('myFunction')) {
   function myFunction(int $x, int $y): int
   {
      return $x + $y;
   }
}

Usage (anywhere in your application):

myFunction(1, 2); // 3
echo MY_FOO;     // bar

Using a Prefix

Prefixes greatly reduce naming conflicts and improve readability (Recommended for Larger Projects).

// /app/Utils/Global.php

define('APP_GLOBAL_MY_FOO', 'bar');

function app_global_my_function(int $x, int $y): int
{
   return $x + $y;
}

Usage:

app_global_my_function(1, 2);
echo APP_GLOBAL_MY_FOO;

Using a Custom Namespace

Namespacing provides the cleanest and safest approach (Best Practice).

// /app/Utils/Global.php

namespace App\Utils\Globals;

// Namespaced constant
const MY_FOO = 'bar';

// Namespaced function
function myFunction(int $x, int $y): int
{
   return $x + $y;
}

Usage:

use const App\Utils\Globals\MY_FOO;
use function App\Utils\Globals\myFunction;

myFunction(1, 2); // 3
echo MY_FOO;     // bar

Recommendations

  • Use simple globals only for small helpers.
  • Use prefixes or namespaces to avoid collisions.
  • Prefer namespaced helpers for long-term maintainability.
  • Avoid turning global helpers into mini-frameworks.

Old rule still applies:

Global scope is convenient, but discipline keeps it safe.