Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Error 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.
Finally 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
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.
The 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
If 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.
that 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.