PHP 8.6.0 Beta 3 is available for testing

Yaconf::get

(PECL yaconf >= 1.0.0)

Yaconf::getRetrieve a configuration value by name

Descrizione

public static function Yaconf::get(string $name, mixed $default = null): mixed

Retrieves the configuration value stored under name. Names use dot notation to traverse nested keys: "app" addresses the whole parsed app.ini file, "app.name" a key inside it, and since Yaconf 1.2.0 "users.database.master" a key in the file database.ini placed in the users/ sub-directory. The dot notation supports up to 64 levels of nesting.

Elenco dei parametri

name

The configuration name to look up, using dot notation to traverse nested keys, for example "app.name", "app.features.1" or, since Yaconf 1.2.0, "users.database.master" for files in sub-directories.

default

The value to return when name is not found. When omitted, null is returned.

Valori restituiti

The stored configuration value, a string or an array, when name exists; otherwise the default (or null when no default was given).

Esempi

The examples below assume the following two files placed in the directory configured with yaconf.directory.

; app.ini
name="shop"                     ; scalar value
debug=0                         ; number
features[]="checkout"           ; array entries, both notations
features.1="wishlist"
; users/database.ini, in a sub-directory (Yaconf 1.2.0+)
master="192.168.0.10"
replica="192.168.0.20"

Example #1 Yaconf::get() example

<?php
// fetch a whole parsed file as an array
var_dump(Yaconf::get("app"));

// dot notation traverses nested keys
var_dump(Yaconf::get("app.name"));           // string(4) "shop"
var_dump(Yaconf::get("app.features.1"));     // string(8) "wishlist"

// since 1.2.0: files in sub-directories are namespaced by the
// directory name
var_dump(Yaconf::get("users.database.master")); // string(12) "192.168.0.10"

// when the key is missing, the default is returned
var_dump(Yaconf::get("app.missing"));             // NULL
var_dump(Yaconf::get("app.missing", "fallback")); // string(8) "fallback"
?>

Vedere anche:

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top