TheFilmGod Posted March 29, 2010 Share Posted March 29, 2010 // EMAIL class validateEmail { // Grab mysqli class into scope global $mysqli; ... validate email ... check if email already exists in database .... How can I access the $mysqli class within the validateEmail class? is there a way of doing this? I don't want to have to connect to the database again. Quote Link to comment https://forums.phpfreaks.com/topic/196897-access-mysqli-class-within-another-class/ Share on other sites More sharing options...
the182guy Posted March 29, 2010 Share Posted March 29, 2010 I think what you're looking for is called the singleton design pattern. One simple option is to store the mysqli object in a $GLOBALS variable. Then you can easily access the database from anywhere like $GLOBALS['DB']->query(); There are more complicated solutions, have a look at this link: http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/196897-access-mysqli-class-within-another-class/#findComment-1033690 Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 Just pass it to the constructor Edit: why does a validation class need access to the database? Quote Link to comment https://forums.phpfreaks.com/topic/196897-access-mysqli-class-within-another-class/#findComment-1033699 Share on other sites More sharing options...
TheFilmGod Posted March 29, 2010 Author Share Posted March 29, 2010 Just pass it to the constructor Edit: why does a validation class need access to the database? Can you elaborate on how I can pass it to the constructor? The validation class needs access to the database to check if the email address is in the "blacklist" - if the email address has been unsubscribed permanently. I wouldn't want users to contact me through the online contact form and use an email that is "blacklisted" on my website's unsubscribed list, because in principle, I shouldn't be sending them emails EVER... unless they resubscribe of course. Quote Link to comment https://forums.phpfreaks.com/topic/196897-access-mysqli-class-within-another-class/#findComment-1033702 Share on other sites More sharing options...
KevinM1 Posted March 29, 2010 Share Posted March 29, 2010 Just pass it to the constructor Edit: why does a validation class need access to the database? Can you elaborate on how I can pass it to the constructor? class Email { private $db; public function __construct($db) { $this->db = $db; } } $mysqli = new mysqli(/* connection string */); $email = new Email($mysqli); Quote Link to comment https://forums.phpfreaks.com/topic/196897-access-mysqli-class-within-another-class/#findComment-1033740 Share on other sites More sharing options...
ignace Posted March 29, 2010 Share Posted March 29, 2010 The validation class needs access to the database to check if the email address is in the "blacklist" Maybe you can take an alternative path as it feels somehow wrong to let a validation class pull records from a database. How about whitelisting? Like below: $subscribed = $usersTable->findBySubscriptionStatus('subscribed');//this line alone should give you the e-mails you need. if (sizeof($subscribed)) { $validEmail = new ValidateEmail(); if ($validEmail->massValidate($subscribed)) {//all e-mails Quote Link to comment https://forums.phpfreaks.com/topic/196897-access-mysqli-class-within-another-class/#findComment-1033741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.