Hello to everybody!
I have began a little practice project after I have finished the book Php From Novice to Ninja. Like in the book, I have set up the database connection and a functions file, which you can see
With this setup, I have to include the db.php file inside every function to be able to use $pdo. I am wondering, is there a way to set up the database connection code so I don’t have to include the db.php in every function?
Thank you very much!
Best practice would be to put it into a class and then autoload that either with spl_autoload_register or to use something like https://getcomposer.org with PSR-0/PSR-4 autoloading.
However PHP namespaces can work on functions as well so no reason why you couldnt just wrap your function in a namespace and spl_autoload_register that. http://php.net/manual/en/function.spl-autoload-register.php
More About Php Fig (PSR 0-7). http://www.php-fig.org
PHP Modern Practices: www.phptherightway.com
Pass it as parameter to the function?
function login_user($email, $password, $pdo)
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.