Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Fatal error when trying to define constants based on the environment #9387

Closed
Sunchock opened this issue Jan 8, 2025 · 4 comments
Closed

Comments

@Sunchock
Copy link

Sunchock commented Jan 8, 2025

PHP Version

8.3

CodeIgniter4 Version

4.5.7

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

No response

What happened?

I would like to define some constants based on my environment but this give me a Fatal error because 'ENVIRONMENT' is not defined.

Constants are loaded before the environment as you can see in Boot.php L131-132

public static function preload(Paths $paths): void
{
static::definePathConstants($paths);
static::loadConstants();
static::defineEnvironment();
static::loadEnvironmentBootstrap($paths, false);
static::loadAutoloader();
}

Is this an expected design? Can we load constants after environment setup?

Steps to Reproduce

Add condition like if (ENVIRONMENT === 'development') { define('A', expr); } else { define('B', expr); } in Config/Constants.php

Expected Output

I would like my constant to be defined according to the environment

Anything else?

No response

@Sunchock Sunchock added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 8, 2025
@Sunchock Sunchock changed the title Bug: Constants are loaded before environment Bug: Fatal error when trying to define constants based on the environment Jan 8, 2025
@neznaika0
Copy link
Contributor

neznaika0 commented Jan 8, 2025

Try as if (env('CI_ENVIRONMENT', 'production') === 'development')

@Sunchock
Copy link
Author

Sunchock commented Jan 8, 2025

Try as if (env('CI_ENVIRONMENT', 'production') === 'development')

I got : Fatal error: Call to undefined function env()

@samsonasik
Copy link
Member

You can register more constants on per-config files (production.php, development.php, testing.php) under Boot directory that verify based environment used

https://github.com/codeigniter4/CodeIgniter4/tree/develop/app/Config/Boot

@samsonasik
Copy link
Member

Closing as answered above #9387 (comment)

@michalsn michalsn removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants