Jump to content

Recommended Posts

// 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.

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

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.

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);

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.