# Standard library

Beside the custom libraries you can use and create in Wolff, there’s the standard library with some useful common functions.

These functions are accessible from anywhere inside Wolff just like the native PHP functions.

The standard library can be disabled, meaning that its functions won't be available in the global namespace and won't collide with other functions of the same name. To disable it, set to false the stdlib_on key in the config array of the system/config.php file.



path([string $path])

Returns the absolute path of the given relative path. Keep in mind that the given path is supposed to be relative to the project root folder.


If the wolff project is located at /var/www/html/wolff, it will return /var/www/html/wolff/app/controllers.

Is associative

isAssoc(array $arr)

Returns true if the given array is associative, false otherwise.

Keep in mind that in the context of the function, an associative array is an array with non-numeric keys.

$arr = [
    'name' => 'Margaret',
    'age'  => 63


That should return true.

isAssoc([ 'Margaret', 'Thomas', 'Edward' ]);

That should return false.


val(array $arr[, string $key])

Returns the specified key from the given array. If the given key doesn't exists it will simply return null.

$arr = [
    'name' => 'Margaret',
    'age'  => 63

val($arr, 'name');

That should return the string Margaret.

The key accepts dot notation:

val($arr, 'user.name');

That should be the equivalent to $arr['user']['name'].

Array remove

arrayRemove(array &$arr, $val)

Removes an element from the given array, based on its value.

It returns true if the element has been removed, false otherwise.

$ships = [
    'mauretania', 'lusitania', 'queen_mary'

arrayRemove($ships, 'lusitania');

After that, the ships variable should look like this:

    [0] => mauretania
    [2] => queen_mary

Keep in mind that the array's keys are preserved even for non-assosiative arrays.

Bytes to string

bytesToString($size[, $precision])

Returns the given size (in bytes) as a human-readable string. The default precision is 2.


That example should return '527KB'.

bytesToString('10000000', 3)

That example should return '9.537MB'.

The human-readable string to return can go from B (byte) to YB (yottabyte).


average(array $arr)

Returns the average value of the given numbers array.

average([ 2.5, 5.46, 4, 9 ]);

That should return 5.24.

Echo and die


Echo a variable and then die (for debugging purposes).

echod('Lorem ipsum dolor sit amet');

Multiple parameters can be passed.

echod('Lorem', 'ipsum', 'dolor');



Prints the given variables in a nice looking way.

$array = ['laravel', 'codeigniter', 'wolff', 'yii'];

This function can take any number of parameters.

printr($array, $foo, $foo2...);

Print and die


Prints the given variables in a nice looking way and then die.

$array = ['laravel', 'wolff', 'yii'];

This function can take any number of parameters.

printrd($array, $foo, $foo2...);

Var dump and die


Var dump a variable and then die (for debugging purposes).

$str = 'Hello world';

Validate CSRF


Returns true if the current request is safe from csrf (cross site request forgery), false otherwise.

This simply verifies that the current user is the one who made the request to the application.

if (validateCsrf()) {
    echo 'The incoming form was made by the user, continue :)';
    // Code
} else {
    echo 'You shall not pass';

This can be combined with the @csrf tag available in the template engine.

If you don't want to turn on the standard library just for using this function, you can make your own implementation.

function validateCsrf()
    // Get
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        return isset($_GET['__token'], $_COOKIE['__token']) && $_GET['__token'] === $_COOKIE['__token'];

    // Post
    return isset($_POST['__token'], $_COOKIE['__token']) && $_POST['__token'] === $_COOKIE['__token'];

Is int


Returns true if the given parameter complies with an integer, false otherwise.


Both of the calls showed above will return true.

Is float


Returns true if the given parameter complies with a float, false otherwise.


Both of the calls showed above will return true.

Is bool


Returns true if the given parameter complies with an boolean, false otherwise.


Both of the calls showed above will return true.

Only the numeric values 1 and 0, and the strings 'true', 'false', '1' and '0' are counted as boolean.

Is json

isJson(string $str)

Returns true if the given string is a json, false otherwise.

_Notice: This function modifies the json_last_error value._

$json = '{name: "John", age: 21, city: "New York"}';

That will return true.

To array


Returns the given variable as an associative array.

Useful when it is necessary to turn a multidimensional json or std object into an array.

$json = '{name: "John", age: 21, city: "New York"}';

Get url

url([string $url])

Returns the given string as a local url. Useful for redirections.



If the project is located at http://localhost/wolff, the function will return http://localhost/wolff/home/sayHello.

If the project is located at https://www.getWolff.com, the function will return https://www.getWolff.com/home/sayHello.

Get client Ip


Returns the current client IP.


In localhost it will return ::1.



Returns true if the current request is running in localhost, false otherwise.


Get current page


Returns the current url.


Get pure current page


Returns the current url without parameters.


If the current url is example.com/homepage?id=2 it will return example.com/homepage.

Get benchmark time


Returns the time between the page load start and the current time in seconds as float.


Get public

getPublic([string $path])

Returns the absolute public path of the given path.


If the project is located at /var/www/html/wolff that should return /var/www/html/wolff/public/favicon.ico.

Get config

config(string $key)

Returns the specified key value from the CONFIG array, or from the environment file if env_override is set to true.


The key accepts dot notation.


Get Wolff version


Returns the current version of Wolff.

//In this case it should return 2.0

Adding functions to the Standard library

You can add your own functions to the standard library this way.

  1. Create a php file with the following structure in the system folder:

namespace {
    function example() {
  1. Add all the functions you want.

  2. After that add the following line to your composer.json file inside the autoload > files array

  1. Remember to run composer dump-autoload for the changes to take effect.

And you are done, now your functions are available inside all your Wolff project.

Documentation made with
Something is wrong in here? Make a issue/pull request on the repo.