Hello everyone,
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:
My Questions
- Should I make the validation inside the
send()
method or inside_generateMsg()
? - Is this code can be considered oop php?
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.
Thanks in advance.