Common Functions and Error Handling
LavaLust comes with several core functions to handle class loading, configuration, error handling, and environment checks. These functions are globally available and form the backbone of the framework.
Class Loader
- load_class(string $class, string $directory = '', array $params = NULL, string $object_name = NULL) object
Loads a class from the specified directory and stores an instance in the Registry.
- Parameters:
$class (string) – The class name to load
$directory (string) – Directory to search in (relative to app/system)
$params (array|null) – Constructor parameters (if any)
$object_name (string|null) – Optional object key for Registry storage
- Return object:
The loaded class instance
Example:
<?php $logger = load_class('Logger', 'kernel'); $logger->log('info', 'This is a test message.');
- loaded_class(string $class = '', string $object_name = '') array
Keeps track of loaded libraries and classes.
- Parameters:
$class (string) – Class name
$object_name (string) – Object key used in Registry
- Return array:
Array of loaded class names
Example:
<?php $loaded = loaded_class(); print_r($loaded);
Error Handling
- show_404(string $heading = '', string $message = '', string $template = '') string
Displays a 404 Page Not Found error page.
- Parameters:
$heading (string) – Optional heading text
$message (string) – Optional detailed message
$template (string) – Optional template name
- Return string:
HTML of the 404 page
Example:
<?php show_404('Page Not Found', 'The requested page does not exist.');
- show_error(string $heading = '', string $message = '', string $template = 'error_general', int $code = 500) string
Displays an error page for debugging purposes.
- Parameters:
$heading (string) – Error heading
$message (string) – Error message
$template (string) – Template file to use
$code (int) – HTTP status code
- Return string:
HTML of the error page
Example:
<?php show_error('Database Error', 'Unable to connect to the database', 'error_db', 500);
Configuration Access
- get_config() array
Loads the global configuration file
app/config/config.phpand returns its array.- Return array:
Associative array of configuration items
Example:
<?php $config = get_config(); echo $config['base_url'];
- config_item(string $item) mixed
Returns a single configuration value from the loaded config.
- Parameters:
$item (string) – Config item key
- Return mixed:
Config value or NULL if not set
Example:
<?php echo config_item('base_url');
- autoload_config() array
Loads the
app/config/autoload.phpfile for auto-loading resources.- Return array:
Array of resources to autoload
Example:
<?php $autoload = autoload_config(); print_r($autoload['libraries']);
- database_config() array
Loads the database configuration file
app/config/database.php.- Return array:
Database configuration array
Example:
<?php $db_config = database_config(); echo $db_config['default']['hostname'];
- route_config() array
Loads the routing configuration file
app/config/routes.php.- Return array:
Routing configuration array
Example:
<?php $routes = route_config(); echo $routes['default_controller'];
Utilities
- html_escape(mixed $var, bool $double_encode = TRUE) mixed
Escapes HTML entities in a string or array recursively.
- Parameters:
$var (mixed) – String or array to escape
$double_encode (bool) – Prevents double-encoding if FALSE
- Return mixed:
Escaped string or array
Example:
<?php echo html_escape('<script>alert(1)</script>');
- is_php(string $version) bool
Checks if the current PHP version is greater than or equal to the specified version.
- Parameters:
$version (string) – PHP version to check
- Return bool:
TRUE if current PHP version is >= $version
Example:
<?php if (is_php('8.0')) { echo 'Running PHP 8+'; }
- is_https() bool
Determines whether the application is accessed via HTTPS.
- Return bool:
TRUE if HTTPS, FALSE otherwise
Example:
<?php if (is_https()) { echo 'Secure connection'; }
Framework Instance Access
- lava_instance() object
Returns the main LavaLust framework instance. This instance allows access to core components such as db, security, call, and other libraries loaded into the framework.
- Return object:
The main LavaLust instance
Example:
<?php // Get the main instance $LAVA = lava_instance(); // Access the database $users = $LAVA->db->table('users')->get(); // Access security methods $csrf_token = $LAVA->security->get_csrf_hash(); // Load a library $logger = $LAVA->load->library('logger');