开发者

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 thin开发者_如何学Pythonk 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...


As you have asked about considering this code as OOP php one, I'd like to share my way of going with OOP php.

I create separate class files with .class extension. This class file roughly has the following structure.

    class classname
    {
    var $var1;  //defining variables
    var $var2;
    .
    .
    var $varn;

    public function __construct($parm1,$param2,....$paramn)
    {
    //intialise variables

    $this->var1=$param1;
    .
    .
    $this->varn=$paramn;
    }

    //give definitions of all the functions here

    public function functionname($param1,$param2...$paramn)

    $city=$param1;
    $country=$param2;
    {
    /*include database connection file(created as db_conf) or explicitly write connection code(not preferable)*/

    //give function definition, manipulation, expresstions and sql queries

    $query1="INSERT INTO city(name,country) VALUES('$city','$country')";
    $result=mysql_query($query1);

return value;
    }

    //similarly define other functions too


    }

Now if you want to use the same script for two or more forms you need to include the class files there and call the appropriate methods. For e.g.

<?php
include_once './classname.class.php'  //give complete path of the class file.
$obj=new classname($par1,...,$parn); //create object

$ret=$obj->functionname($par1,...,$parn);

?>

I hope this was helpful :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜