【发布时间】:2010-08-27 21:57:49
【问题描述】:
我正在尝试使用我的一个脚本进行 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 的方式重写代码后,我现在可以在不破坏代码的情况下以多种顺序使用方法,这就是为什么我很困惑何时何地使用它的最佳位置。
【问题讨论】:
标签: php oop coding-style