shinepress / globals
Allow ShinePress modules to register global functions and variables.
Requires
- php: >=8.1
- shinepress/framework: ^1.0
Requires (Dev)
- php-cs-fixer/shim: ^3.75
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.0
- shinepress/coding-standard: dev-main
This package is auto-updated.
Last update: 2025-05-19 20:04:13 UTC
README
Description
Add-on for shinepress/framework to allow registering of global variables, functions, and constants for use outside of modules.
Installation
The recommendend installation method is with composer:
$ composer require shinepress/globals
Usage
Add the 'RegisterGlobal' attribute to any module class/property/constant/method to register it into the global namespace.
Module Instance
use ShinePress\Framework\Module; use ShinePress\Globals\RegisterGlobal; #[RegisterGlobal('module')] // registers as $module #[RegisterGlobal] // registers as $ExampleModule class ExampleModule extends Module { public function hello(): string { return 'world'; } } ExampleModule::register();
global $module; print $module->hello(); // prints: 'world'
global $ExampleModule; print $ExampleModule->hell(); // prints: 'world'
Module Property
Assigns a reference to the indicated class property to the global variable regardless of visibility.
use ShinePress\Framework\Module; use ShinePress\Globals\RegisterGlobal; class ExampleModule extends Module { #[RegisterGlobal('testProperty')] public string $myProperty = 'foo'; } ExampleModule::register();
global $testProperty; print $testProperty; // prints: 'foo'; $testProperty = 'bar'; print ExampleModule::instance()->myProperty; // prints: 'bar'
Module Constant
Defines a global constant with the value of the class constant regardless of visibility.
use ShinePress\Framework\Module; use ShinePress\Globals\RegisterGlobal; class ExampleModule extends Module { #[RegisterGlobal('TEST_CONSTANT')] public const MY_CONSTANT = 'foobar'; } ExampleModule::register();
print TEST_CONSTANT; // prints: 'foobar'
Module Method
Creates a closure reference to the indicated method and assigns it to a function in the global scope.
Note: the relies on eval, with all the associated downsides.
use ShinePress\Framework\Module; use ShinePress\Globals\RegisterGlobal; class ExampleModule extends Module { #[RegisterGlobal('toLowercase')] public function lowercase(string $input): string { return strtolower($input); } #[RegisterGlobal('toUppercase')] public function uppercase(string $input): string { return strtoupper($input); } } ExampleModule::register();
print toLowercase('HELLOWORLD'); // prints: 'helloworld' print toUppercase('helloworld'); // prints: 'HELLOWORLD'