2.1. TConstsClass¶
trait TConstsClass
{
use TStaticClass;
}
The TConstsClass should be used on a class whose sole purpose is to define a collection of constants with a common domain. It’s a good practice to not provide any additional functionality to this class beyond consts definitions.
Note
TConstsClass methods should not be used for input validation unless followed by additional validation rules. If a set of interchangeable values or binary flags is needed, TEnum should be used instead.
Note
TConstsClass methods will return all constants in the class, regardless of their access level, and ignore any other non-const definitions.
class APIEndPoints
{
use TConstsClass;
public const GET_USERS = '/users';
public const GET_CLIENTS = '/clients';
public static $someEndpoint = '/help';
}
APIEndPoints::getConstsAsArray() will return
["GET_USERS" => "/users", "GET_CLIENTS" => "/clients"]
2.1.1. getConstsAsArray¶
-
public static getConstsAsArray(): array Return an associative array of all the consts in the class, using the name as the key and the value as the value.
Example:
class APIEndPoints
{
use TConstsClass;
public const GET_USERS = '/users';
public const GET_CLIENTS = '/clients';
public const DEPRECATED_ENDPOINTS = [
self::GET_CLIENTS
];
}
APIEndPoints::getConstsAsArray() will return
["GET_USERS" => "/users", "GET_CLIENTS" => "/clients", "DEPRECATED_ENDPOINTS" => ["/clients"]]
2.1.2. getConstNames¶
-
public static getConstNames(): array Return an array of the names of all the consts in the class.
Example:
class APIEndPoints
{
use TConstsClass;
public const GET_USERS = '/users';
public const GET_CLIENTS = '/clients';
public const DEPRECATED_ENDPOINTS = [
self::GET_CLIENTS
];
}
APIEndPoints::getConstNames() will return
["GET_USERS", "GET_CLIENTS", "DEPRECATED_ENDPOINTS"]
2.1.3. getConstValues¶
-
public static getConstValues(): array Return an array of the values of all the consts in the class.
Example:
class APIEndPoints
{
use TConstsClass;
public const GET_USERS = '/users';
public const GET_CLIENTS = '/clients';
public const DEPRECATED_ENDPOINTS = [
self::GET_CLIENTS
];
}
APIEndPoints::getConstValues() will return
["/users", "/clients", ["/clients"]]
2.1.4. getConstsCount¶
-
public static getConstsCount(): int Return the count of the consts in the class.
Example:
class APIEndPoints
{
use TConstsClass;
public const GET_USERS = '/users';
public const GET_CLIENTS = '/clients';
public const DEPRECATED_ENDPOINTS = [
self::GET_CLIENTS
];
}
APIEndPoints::getConstsCount() will return
3
2.1.5. isConstExists¶
-
public static public static isConstExists($name): bool Return true if a const with the name $name exists in the class, and false otherwise.
Example:
class APIEndPoints
{
use TConstsClass;
public const GET_USERS = '/users';
public const GET_CLIENTS = '/clients';
public const DEPRECATED_ENDPOINTS = [
self::GET_CLIENTS
];
}
APIEndPoints::isConstExists('GET_USERS') will return
true
APIEndPoints::isConstExists('ENDPOINT') will return
false
2.1.6. isConstValueExists¶
-
public static isConstValueExists($value): bool Return true if a const with a value $value exists in the class, and false otherwise. A strict comparison is used to search for the value.
Example:
class APIConfig
{
use TConstsClass;
public const TIMEOUT = 5;
}
APIEndPoints::isConstValueExists(5) will return
true
APIEndPoints::isConstValueExists('5') will return
false