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

I am trying to execute one of my scripts. This is a contact script that processes encodings when sending emails using the jQuery ajax function.

I wanted the user to be able to use the same script with two forms on the same page and make it an easy job.

Now I have made a prototype of how this will be rewritten to fit.

It was really strange for me, but I study every day. The hardest part for me is where should I place my methods and how to make a thread script.

To demonstrate what I mean, here are some parts of the code I'm using 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 do most of the work for my script. I use it like this:

$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();
}

, :

  • send() _generateMsg()?
  • oop php?

1 , :

, , , oop, , . , .

+5
2

, validate(). , . , , . , , , , , . a coolContact , , , send(). a coolContact , , , _generateMsg() validate()

№ 2, , . - ...

+2

, OOP php one, OOP php.

.class. .

    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


    }

, script , . .

<?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);

?>

, :)

0

All Articles