PHPackages                             rogervila/global-namespace - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rogervila/global-namespace

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rogervila/global-namespace
==========================

Prefix global PHP functions under a namespace

1.1.0(5y ago)4828MITPHPCI passing

Since Jan 23Pushed 2w ago1 watchersCompare

[ Source](https://github.com/rogervila/global-namespace)[ Packagist](https://packagist.org/packages/rogervila/global-namespace)[ RSS](/packages/rogervila-global-namespace/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (2)Versions (6)Used By (0)

[![Build Status](https://github.com/rogervila/global-namespace/workflows/build/badge.svg)](https://github.com/rogervila/global-namespace/actions)[![Example Application Status](https://github.com/rogervila/global-namespace/workflows/example-app/badge.svg)](https://github.com/rogervila/global-namespace/tree/main/examples/random-app-example)[![StyleCI](https://camo.githubusercontent.com/5f88b84c2611f30e2d13b4b4f6584050587bda5347326d7585bd7132c9c21647/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3333323139363039372f736869656c643f6272616e63683d6d61696e)](https://github.styleci.io/repos/332196097)[![Quality Gate Status](https://camo.githubusercontent.com/73a00bd1b841990e69451a6b9939dbf12045fe933b3ea34d958e161f28d6ee1b/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d726f67657276696c615f676c6f62616c2d6e616d657370616365266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=rogervila_global-namespace)

[![Latest Stable Version](https://camo.githubusercontent.com/3c54faf99bec94c11a484b7beba9f632b03272694ebd2bc5541d381ed91fc181/68747470733a2f2f706f7365722e707567782e6f72672f726f67657276696c612f676c6f62616c2d6e616d6573706163652f762f737461626c65)](https://packagist.org/packages/rogervila/global-namespace)[![Total Downloads](https://camo.githubusercontent.com/42bef28e1fffb521f85333a102667043dfae87420aff174a29b95b8991b6d03f/68747470733a2f2f706f7365722e707567782e6f72672f726f67657276696c612f676c6f62616c2d6e616d6573706163652f646f776e6c6f616473)](https://packagist.org/packages/rogervila/global-namespace)[![License](https://camo.githubusercontent.com/58ae98b4af28a8048f1c6f1f69e47520908ce4ab90e1fa9c3ec7ee224f6d26fa/68747470733a2f2f706f7365722e707567782e6f72672f726f67657276696c612f676c6f62616c2d6e616d6573706163652f6c6963656e7365)](https://packagist.org/packages/rogervila/global-namespace)

[![Global Namespace](https://camo.githubusercontent.com/c578d3db19c17f7095dac80af3f88ecff15b13e3d209ab6d7ea55088926d8ff0/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f476c6f62616c2532304e616d6573706163652e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d726f67657276696c61253246676c6f62616c2d6e616d657370616365267061747465726e3d63616765267374796c653d7374796c655f31266465736372697074696f6e3d5072656669782b676c6f62616c2b5048502b66756e6374696f6e732b756e6465722b612b6e616d657370616365266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d68747470732533412532462532467777772e7068702e6e6574253246696d616765732532466c6f676f732532467068702d6c6f676f2e737667)](https://camo.githubusercontent.com/c578d3db19c17f7095dac80af3f88ecff15b13e3d209ab6d7ea55088926d8ff0/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f476c6f62616c2532304e616d6573706163652e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d726f67657276696c61253246676c6f62616c2d6e616d657370616365267061747465726e3d63616765267374796c653d7374796c655f31266465736372697074696f6e3d5072656669782b676c6f62616c2b5048502b66756e6374696f6e732b756e6465722b612b6e616d657370616365266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d68747470732533412532462532467777772e7068702e6e6574253246696d616765732532466c6f676f732532467068702d6c6f676f2e737667)

Global Namespace
================

[](#global-namespace)

This library provides a namespace for functions that are called globally, like PHP Built-in functions.

This might sound weird, so let me introduce an example to show what it can do.

Example
-------

[](#example)

Imagine that you have an application that uses `rand(0, 1)` and you want to assert what happens when it returns `0` or `1`

Let's scaffold that class

```
class App
{
    public function run()
    {
        $result = rand(0, 1);

        if ($result == 0) {
            // do something when $result is 0
        } else {
            // do something else when $result is 1
        }
    }
}
```

If you create a test to assert the results you will find a stopper: **How can we mockup rand() to force it to return 0 or 1?**. This package provides a solution for this question.

First, require this package on your application.

```
composer require rogervila/global-namespace
```

Now, update your application to prefix `rand()` with the global namespace.

**`PHP::rand()` will simply call the built-in PHP function** so your application behaviour will not change.

```
use PHP\PHP;

class App
{
    public function run()
    {
        $result = PHP::rand(0, 1);
        //...
    }
}
```

This works as expected but is not yet fully testeable because the `PHP` dependency is hardcoded and it cannot be mocked.

Let's add it in the constructor so we can use any `PHP` implementation.

```
use PHP\PHP;

class App
{
    private PHP $php;

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

    public function run()
    {
        $result = $this->php::rand(0, 1);
        //...
    }
}
```

Nice. Now you can mockup `rand()` with [Mockery](https://github.com/mockery/mockery) and assert the application code:

```
final class AppTest extends \PHPUnit\Framework\TestCase
{
    public function test_rand_returns_one()
    {
        // Create a mock of the package class
        $php = Mockery::mock(\PHP\PHP::class);

        // Define its return value
        $php->shouldReceive('rand')
            ->once()
            ->andReturn(1);

        // Now rand() always returns 1
        $this->assertEquals(
            1,
            $php::rand()
        );

        // Create the instance with the mocked PHP implementation
        $app = new App($php);

        // Do your application assertions here
        // ...
    }
}
```

> Check this [demo application](https://github.com/rogervila/global-namespace/tree/main/examples/random-app-example) for a more complete example

About
-----

[](#about)

This library calls any global function, not only those that are built-in.

```
use PHP\PHP;

// PHP Built-in functions
PHP::http_build_query(['foo' => 'bar']); // returns 'foo=bar'
PHP::json_encode(['foo' => 'bar']); // returns '{"foo": "bar"}'
PHP::json_decode('{"foo": "bar"}'); // return ['foo' => 'bar']
// etc...

// For WordPress projects
PHP::wp_redirect('/') // redirects to home
// etc...

// Literaly, any function, built-in or not, could be listed here
```

Helper classes
--------------

[](#helper-classes)

If you do not want to use Mockery, or if you are working on test environment that does not fit with it, you may use some helper classes that come with this package

### IgnoreMissing

[](#ignoremissing)

It works the same way, but it will ignore a function if it does not exist.

This can be useful if you have a function that you cannot be mocked for some reason.

```
use PHP\IgnoreMissing as PHP;

PHP::http_build_query(['foo' => 'bar']); // returns 'foo=bar'

PHP::foo(); // returns null instead of triggering a "Call to undefined function" error

// missing functions return null. existing functions are called
```

### IgnoreAlways

[](#ignorealways)

Use it if, for some reason, you want to directly skip all function calls.

It does not matter if the function exists or not.

```
use PHP\IgnoreAlways as PHP;

PHP::http_build_query(['foo' => 'bar']); // returns null

PHP::foo(); // returns null

// all functions return null
```

Author
------

[](#author)

Created by [Roger Vilà](https://rogervila.es)

License
-------

[](#license)

Global Namespace is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance64

Regular maintenance activity

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 65.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

1939d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/351443b7d23e94fcf31b250db90f0b9578cc9fd8e0cefbed9666467e3e9cb571?d=identicon)[rogervila](/maintainers/rogervila)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (28 commits)")[![rogervila](https://avatars.githubusercontent.com/u/6053012?v=4)](https://github.com/rogervila "rogervila (15 commits)")

---

Tags

php global namespace

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rogervila-global-namespace/health.svg)

```
[![Health](https://phpackages.com/badges/rogervila-global-namespace/health.svg)](https://phpackages.com/packages/rogervila-global-namespace)
```

###  Alternatives

[boesing/typed-arrays

Hashmap and Collection

1036.5k1](/packages/boesing-typed-arrays)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
