php - Calling User Defined Function In User Defined Class -


i trying create custom class handle mailing me.

here class (mailerclass.php) :

class mailer {          // private fields         private $to;         private $subject;         private $message;          // constructor function         function __construct($to, $subject, $message) {             $to = $to;             $subject = $subject;             $message = $message;         }             // function send mail constructed data.         public function sendmail() {              if (mail($to, $subject, $messagecontent)) {                 return "success";             }             else {                 return "failed";                 }                    }      } 

when try call here (index.php) "call undefined function sendmail()" message?

if ($_server['request_method'] == "post") {          // import class         include('mailerclass.php');          // trim , store data         $name = trim($_post['name']);         $email = trim($_post['email']);         $message = trim($_post['message']);           // store mail data         if ($validated == true) {              // create new instance of mailer class user data             $mailer = new mailer($to, $subject, $message);                        // send mail , store result             $result = $mailer.sendmail();         } 

why happen ??

you don't call class methods dot. call class methods (not static) -> like:

$result = $mailer->sendmail(); 

besides need set properties $this-> (again if not static) change content of contstructor to:

$this->to = $to; $this->subject = $subject; $this->message = $message; 

same goes mail() function:

mail($this->to, $this->subject, $this->messagecontent) 

you saw me mentioning static few times, if ever want access static property or method in class can use self::


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -