Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would you write this code the same way using OOP PHP?

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

  1. Should I make the validation inside the send() method or inside _generateMsg() ?
  2. 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.

like image 509
Maher4Ever Avatar asked Nov 05 '22 09:11

Maher4Ever


1 Answers

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

like image 50
Josh Avatar answered Nov 11 '22 07:11

Josh