PHPackages                             sebastiaanluca/php-helpers - 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. sebastiaanluca/php-helpers

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

sebastiaanluca/php-helpers
==========================

An extensive set of PHP helper functions and classes.

3.0.0(5y ago)2719.4k↓50%41MITPHPPHP ^8.0

Since Jul 22Pushed 3y ago2 watchersCompare

[ Source](https://github.com/sebastiaanluca/php-helpers)[ Packagist](https://packagist.org/packages/sebastiaanluca/php-helpers)[ Docs](https://github.com/sebastiaanluca/php-helpers)[ RSS](/packages/sebastiaanluca-php-helpers/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (6)Used By (1)

An extensive set of PHP helper functions and classes
====================================================

[](#an-extensive-set-of-php-helper-functions-and-classes)

[![Latest stable release](https://camo.githubusercontent.com/533cc45fbc6553c3e0962e7c31f2503ef1a7267b23eda1f7a155138d89c6d282/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7365626173746961616e6c7563612f7068702d68656c706572732e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/sebastiaanluca/php-helpers)[![Software license](https://camo.githubusercontent.com/08662bb339bee903c7279b82f3217310d3f196fdfa63b019d294ae85e1a5d7d7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d696e666f726d6174696f6e616c2e737667)](LICENSE.md)[![Build status](https://github.com/sebastiaanluca/php-helpers/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/sebastiaanluca/php-helpers/actions/workflows/test.yml?query=branch%3Amaster)[![Total downloads](https://camo.githubusercontent.com/479634464b66a3717560c76f097e9193c0bf09a604ebbbed5a64e57cec2b588f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7365626173746961616e6c7563612f7068702d68656c706572732e7376673f636f6c6f723d627269676874677265656e)](https://packagist.org/packages/sebastiaanluca/php-helpers)[![Total stars](https://camo.githubusercontent.com/f3ddc4e03d465116e964d483d0b66a1df81d646f84505174e44c06e3b05d880c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f7365626173746961616e6c7563612f7068702d68656c706572732e7376673f636f6c6f723d627269676874677265656e)](https://github.com/sebastiaanluca/php-helpers)

[![Read my blog](https://camo.githubusercontent.com/0760b582e22202bd412feb64f5f87b6459f64a0b8ea5fdd24cbadf87df736177/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d626c6f672d6c69676874677265792e737667)](https://sebastiaanluca.com/blog)[![View my other packages and projects](https://camo.githubusercontent.com/5ce3a83ee0ec2b4ae47a99f67347c1d8e78582757eca80fe31c98e5bd0e2185a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d6f746865725f7061636b616765732d6c69676874677265792e737667)](https://packagist.org/packages/sebastiaanluca)[![Follow @sebastiaanluca on Twitter](https://camo.githubusercontent.com/bde346ed8efc8b2458386b36256c64e3fe010276f449b80fe4f663c4c8bbd83e/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f7365626173746961616e6c7563612e7376673f7374796c653d736f6369616c)](https://twitter.com/sebastiaanluca)[![Share this package on Twitter](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=Check%20out%20this%20extensive%20set%20of%20generic%20PHP%20helper%20functions%20and%20classes!%20Via%20@sebastiaanluca%20https://github.com/sebastiaanluca/php-helpers)

Table of contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [How to install](#how-to-install)
- [Global helper functions](#global-helper-functions)
    - [rand\_bool](#rand_bool)
    - [str\_wrap](#str_wrap)
    - [is\_assoc\_array](#is_assoc_array)
    - [array\_expand](#array_expand)
    - [array\_without](#array_without)
    - [array\_pull\_value](#array_pull_value)
    - [array\_pull\_values](#array_pull_values)
    - [array\_hash](#array_hash)
    - [object\_hash](#object_hash)
    - [has\_public\_method](#has_public_method)
    - [carbon](#carbon)
    - [temporary\_file](#temporary_file)
- [Class helpers](#class-helpers)
    - [Constants trait](#constants-trait)
        - [Retrieving constants](#retrieving-constants)
        - [Retrieving constant keys](#retrieving-constant-keys)
        - [Retrieving constant values](#retrieving-constant-values)
    - [ProvidesClassInfo trait](#providesclassinfo-trait)
    - [MethodHelper](#methodhelper)
- [License](#license)
- [Change log](#change-log)
- [Testing](#testing)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [About](#about)

Requirements
------------

[](#requirements)

- PHP 8 or higher

How to install
--------------

[](#how-to-install)

Via Composer:

```
composer require sebastiaanluca/php-helpers
```

All function helpers will be enabled by default (if those functions haven't already been defined). Class helpers are enabled per-case when used.

You can find more info on how to use a helper and what there requirements are in their respective section (see the table of contents above for an overview).

Global helper functions
-----------------------

[](#global-helper-functions)

### rand\_bool

[](#rand_bool)

Randomly return `true` or `false`.

```
rand_bool();

// true
```

### str\_wrap

[](#str_wrap)

Wrap a string with another string.

```
str_wrap('foo', '*');

// "*foo*"
```

### is\_assoc\_array

[](#is_assoc_array)

Check if an array is associative.

Performs a simple check to determine if the given array's keys are numeric, start at 0, and count up to the amount of values it has.

```
is_assoc_array(['color' => 'blue', 'age' => 31]);

// true
```

```
is_assoc_array([0 => 'blue', 7 => 31]);

// true
```

```
is_assoc_array(['blue', 31]);

// false
```

```
is_assoc_array([0 => 'blue', 1 => 31]);

// false
```

### array\_expand

[](#array_expand)

Expand a flat dotted array into a multi-dimensional associative array.

If a key is encountered that is already present and the existing value is an array, each new value will be added to that array. If it's not an array, each new value will override the existing one.

```
array_expand(['products.desk.price' => 200]);

/*
[
    "products" => [
        "desk" => [
            "price" => 200,
        ],
    ],
]
*/
```

### array\_without

[](#array_without)

Get the array without the given values.

Accepts either an array or a value as parameter to remove.

```
$cars = ['bmw', 'mercedes', 'audi'];
$soldOut = ['audi', 'bmw'];

$inStock = array_without($cars, $soldOut);

// ["mercedes"]
```

```
array_without(['one', 'two', 'three'], 'two');

// ["one", "three"]
```

### array\_pull\_value

[](#array_pull_value)

Pull a single value from a given array.

Returns the given value if it was successfully removed from the source array or `null` if it was not found.

```
$source = ['A', 'B', 'C'];

$removed = array_pull_value($source, 'C');

// $removed = "C"
// $source = ["A", "B"]
```

### array\_pull\_values

[](#array_pull_values)

Pull an array of values from a given array.

Returns the values that were successfully removed from the source array or an empty array if none were found.

```
$source = ['A', 'B', 'C'];
$removed = array_pull_values($source, ['A', 'B']);

// $removed = ["A", "B"]
// $source = ["C"]
```

### array\_hash

[](#array_hash)

Create a unique string identifier for an array.

The identifier will be entirely unique for each combination of keys and values.

```
array_hash([1, 2, 3]);

// "262bbc0aa0dc62a93e350f1f7df792b9"
```

```
array_hash(['hash' => 'me']);

// "f712e79b502bda09a970e2d4d47e3f88"
```

### object\_hash

[](#object_hash)

Create a unique string identifier for an object.

Similar to [array\_hash](#array_hash), this uses `serialize` to *stringify* all public properties first. The identifier will be entirely unique based on the object class, properties, and its values.

```
class ValueObject {
    public $property = 'randomvalue';
}

object_hash(new ValueObject);

// "f39eaea7a1cf45f5a0c813d71b5f2f57"
```

### has\_public\_method

[](#has_public_method)

Check if a class has a certain public method.

```
class Hitchhiker {
    public function answer() {
        return 42;
    }
}

has_public_method(Hitchhiker::class, 'answer');

// true

has_public_method(new Hitchhiker, 'answer');

// true
```

### carbon

[](#carbon)

Create a Carbon datetime object from a string or return a new object referencing the current date and time.

Requires the [nesbot/carbon](https://github.com/briannesbitt/Carbon) package.

```
carbon('2017-01-18 11:30');

/*
Carbon\Carbon {
    "date": "2017-01-18 11:30:00.000000",
    "timezone_type": 3,
    "timezone": "UTC",
}
*/

carbon();

/*
Carbon\Carbon {
    "date": "2017-10-27 16:18:00.000000",
    "timezone_type": 3,
    "timezone": "UTC",
}
*/
```

### temporary\_file

[](#temporary_file)

Create a temporary file.

Returns an array with the file handle (resource) and the full path as string.

The temporary file is readable and writeable by default. The file is automatically removed when closed (for example, by calling fclose() on the handle, or when there are no remaining references to the file handle), or when the script ends.

See [](http://php.net/manual/en/function.tmpfile.php) for more information.

```
temporary_file();

/*
[
    "file" => stream resource {
        timed_out: false
        blocked: true
        eof: false
        wrapper_type: "plainfile"
        stream_type: "STDIO"
        mode: "r+b"
        unread_bytes: 0
        seekable: true
        uri: "/tmp/phpxm4bcZ"
        options: []
    }
    "path" => "/tmp/phpxm4bcZ"
]
*/
```

Class helpers
-------------

[](#class-helpers)

### Enum trait

[](#enum-trait)

The primary use of the `Enum` trait is to enable you to store all cases of a specific type in a single class or value object and have it return those with a single call.

This can be useful for instance when your database uses integers to store states, but you want to use descriptive strings throughout your code (i.e. enums). It also allows you to refactor these elements at any time without having to waste time searching your code for any raw values (and probably miss a few, introducing new bugs along the way).

#### Retrieving elements

[](#retrieving-elements)

Returns an array of element keys and their values.

```
