Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialUwem Uke
350 PointsError outputting echo getenv('SECRET');
Following to the letter, instructions in the video to output "echo getenv('SECRET');", i am unable to resolve the following error:
Catchable fatal error: Argument 1 passed to Dotenv\Dotenv::__construct() must be an instance of Dotenv\Loader, string given, called in C:\xampp\htdocs\project\inc\bootstrap.php on line 3 and defined in C:\xampp\htdocs\project\vendor\vlucas\phpdotenv\src\Dotenv.php on line 31
Hope i can be pointed to what i'm missing here, thanks.
4 Answers
Uwem Uke
350 PointsFinally found the answer. As per documentation on latest upgrade from V2: "you will need to replace any occurrences of 'new Dotenv(...)' with 'Dotenv::create(...)'". Hope it helps another.
require DIR . '/../vendor/autoload.php';
//$dotenv = new Dotenv\Dotenv(DIR); //Old version
$dotenv = Dotenv\Dotenv::create(DIR); //New version as of date
$dotenv->load();
P.S. For reasons unknown, in this answer, the DIR keyword has been stripped of its double underscores(_ _) on both sides. It should be formatted accordingly.
Chapman Cheng
PHP Development Techdegree Graduate 46,936 PointsThe package probably got updated again. the current usage as of July 2020 is
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
make sure to check documentaion
jpizzle
36,395 PointsIf anyone is having trouble echo'ing out the SECRET environment variable, change Dotenv::createImmutable in the bootstrap file to Dotenv::createUnsafeImmutable
Found Here under "Putenv and Getenv" section: https://github.com/vlucas/phpdotenv#:~:text=PHP%20dotenv%20is%20a%20PHP,version%20of%20the%20original%20Ruby%20dotenv.
jamesjones21
9,260 Pointsthat basically means it needs to be an instance of the Dotenv\Loader class, so the variable you are sending into the getenv(); needs to be an object that uses the Dotenv\Loader class.
Uwem Uke
350 PointsThank you jamesjones21. The variable in getenv(); comes from a .env file, which is explicitly called by the Dotenv\Dotenv class as per video instruction. However, nothing i have tried so far works, and i see phpdotenv has been updated since that video. I am not a code monkey, and this is not what i will pay for. Would have loved that the video instructions were updated to reflect current implementations. If you will, I'm sure you can replicate the issue if you follow the video. Thanks again.
Stef Lynn-Smith
16,868 PointsStef Lynn-Smith
16,868 PointsI couldn't get this to work with ::create but it works with ::createImmutable
$dotenv = Dotenv\Dotenv::createImmutable(DIR); $dotenv->load();