Home » Php » php – If stament in a class call

php – If stament in a class call

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to put an if statement when calling a class, but the operator -> produces a problem I cant figure out. So calling the class like:

$email = WP_Mail::init()
      ->to('[email protected]')
      ->subject('This is an automated message pleas do not reply')
      ->template(plugin_dir_path( __DIR__ ) .'email-templates/email.php', [
        'name' => 'Anthony Budd',
        'email' => '[email protected]',
        'skills' => [
           'PHP',
           'AWS',
        ] 
    ]);
    if (site_url() === 'mysite'){
      ->send();
    }else {
      ->render();
      echo $email;
    }

normally it would look like:

$email = WP_Mail::init()
  ->to('[email protected]')
  ->template(get_template_directory() .'/emails/demo.php', [
  'name' => 'Anthony Budd',
  'location' => 'London',
  'skills' => [
       'PHP',
       'AWS',
    ] 
])
->send();

this above produces an error due to the operator, how can I return “->render()” or “->send()”. I want to display the data if on my dev computer but when I push the code to me server it needs to send the email.

How to&Answers:

You’re missing the object $email before calling the methods. Take a look at this:

if (site_url() === 'mysite'){
  $email->send();
}else {
  $email->render();
  echo $email;
}