I'm trying to go oop with one of my scripts. It's a contact script which deals with encodings when sending an email through jQuery's ajax function.
I wanted to make the user able to use the same script with two forms in the same page and making it an easy job.
Now I've made a prototype of how it's going to be rewritten with oop in mind.
It's been really confusing to me, but I'm learning everyday. The most difficult part for me is where should I place my methods and how to make the flow of the script.
To demonstrate what I mean, here are some parts of the code I use now :
/*
* Start defining some vars at the runtime
*/
public function __construct($data, $config = array()) {
$lang = isset($config['language']) ? $config['language'] : 'en';
$this->_getPhrases($lang);
$this->_recieverEmail = isset ($config['reciever_email']) ? filter_var($config['reciever_email'], FILTER_SANITIZE_EMAIL) : die($this->_phrase['noRecieverEmail']);
$this->_ajax = ($this->_enableAjax($config['ajax_enabled'])) ? true : false;
$this->_data = isset($data) ? (is_array($data) ? $data : die($this->_phrase['errors']['dataNotArray'])) : $_POST;
}
/*
* Send the message
*/
public function send() {
if(!$this->isDataVaild($this->_data)) {
return false;
}
$this->_data = $this->_cleanData($this->_data);
$this->setSenderName($this->_data['name']);
$this->setSenderEmail($this->_data['email']);
$this->_message = $this->_generateMsg($this->data);
$PHPMailer = new PHPMailerLite();
$this->_sendUsing($PHPMailer, $this->_message);
return true;
}
I chose these two methods specifically because they are doing most of the work for my script. I use it this way :
$config = array(
'language' => 'en',
'ajax_enabled' => false,
'reciever_email' => 'recieve@localhost'
);
$contact = new coolContact($_POST, $config);
if($contact->send()) {
echo 'Message sent with No problems';
} else {
echo $contact->getErrors();
}
After all of this, here are my questions:
send()
method or inside _generateMsg()
?Question 1 can be strange to some people, so let me explain it:
After rewriting the code with what I think is oop, I can now use the methods in many orders without breaking the code, That's why I'm confused when and where is the best place to use it.
If I were you, I would place the validation inside it's own method, validate()
. That way you can call that code from any function. Try to keep each method as short and sweet as possible, and combine methods into larger methods whenever possible. It looks like you might have already made the validation it's own function, in which case where you call it depends on where in your logic the data needs to be validated. If a coolContact
can exist with invalid data, but can't be sent until the data is corrected, then validate in send()
. If a coolContact
must have valid data, period, it can never exist with invalid data, then _generateMsg()
should call validate()
As for #2, I would consider this OOP, yes. But both my answers here are opinions...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With