我正试着用我的一个脚本去oop.这是一个联系脚本,通过jQuery的ajax函数发送电子邮件时处理编码.
我想让用户能够在同一页面中使用相同的脚本和两个表单,并使其成为一项简单的工作.
现在我已经制作了一个原型,它将如何用oop重写它.
这对我来说真的很困惑,但我每天都在学习.对我来说最困难的部分是我应该在哪里放置我的方法以及如何制作脚本流程.
为了演示我的意思,这里是我现在使用的代码的一些部分:
/*
* 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;
}
我特意选择了这两种方法,因为它们正在为我的脚本完成大部分工作.我用这种方式:
$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重写代码之后,我现在可以在不破坏代码的情况下使用许多顺序中的方法,这就是为什么我很困惑何时何地最好的地方使用它.