Constructor-Like Initialization Without Classes
Initializes a procedural configuration array with defaults, overrides, validation, and a ready flag without using a class constructor.
Purpose
Initializes a procedural configuration array with defaults, overrides, validation, and a ready flag without using a class constructor.
Snippet details
ContextConfigurationLevelProductionCopy-and-paste statusMarked safe after review.Categories
- Forms and Validation
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Constructor-Like Initialization Without Classes.
*
* Purpose:
* Builds a ready-to-use configuration array through a procedural initializer.
*
* @param array $options Caller-provided overrides.
* @return array Initialized configuration with validation messages.
*/
function ogSnippetConstructorLikeInitializationWithoutClasses(array $options): array {
$config = array(
'name' => 'mission-control',
'timezone' => 'UTC',
'max_events' => 25,
'enabled' => true,
'errors' => array(),
'ready' => false
);
if (isset($options['name']) === true) {
$name = trim((string) $options['name']);
if ($name !== '') {
$config['name'] = $name;
}
}
if (isset($options['timezone']) === true) {
$timezone = trim((string) $options['timezone']);
if (in_array($timezone, timezone_identifiers_list(), true) === true) {
$config['timezone'] = $timezone;
} else {
$config['errors'][] = 'Invalid timezone.';
}
}
if (isset($options['max_events']) === true) {
$max_events = (int) $options['max_events'];
if ($max_events >= 1 && $max_events <= 500) {
$config['max_events'] = $max_events;
} else {
$config['errors'][] = 'Maximum events must be between 1 and 500.';
}
}
if (isset($options['enabled']) === true) {
$config['enabled'] = (bool) $options['enabled'];
}
if (count($config['errors']) === 0 && $config['enabled'] === true) {
$config['ready'] = true;
}
return $config;
}
$stargate_config = ogSnippetConstructorLikeInitializationWithoutClasses(array(
'name' => 'alpha-site-dialer',
'timezone' => 'America/Denver',
'max_events' => 38,
'enabled' => true
));
$initializer_ready = 'no';
if ($stargate_config['ready'] === true) {
$initializer_ready = 'yes';
}
echo 'Initializer ready: '.$initializer_ready;