前几天放假,出去玩了,现在继续这个系列
前两回:
php新手阳光系列:写给新手的话 http://www.phpchina.com/bbs/thread-64864-1-1.html
php新手阳光系列:善用工具 http://www.phpchina.com/bbs/thread-64868-1-1.html
ps:我不是什么大虾,就一个快毕业的学生,php方面就做过几个简单的项目,学php纯粹是因为兴趣学的:)现在在公司实习,工作主要是.NET相关,以后可能很少碰php了,所以想把以前做php项目时的经验与大家分享,希望对新手有用!
许多功能是每个系统中都能用到的,比如验证码,邮件发送,计数器,smarty,分页,登录……
将这些常用的工具类收集整理出来,并且根据工具类写出适当的接口函数,这样能提高对你项目开发的速度
下面是几个示例
网上有许多功能类,我们完全可以拿来使用,当然不是简单的直接使用,我们需要根据它建立自己能方便调用的接口。比如一个smtp邮件发送程序,需要提供服务器,用户名等参数,直接用的话不太方便,所以我们照下面的方式来处理
首先是根据smtp类写一个邮件发送类require("smtp.php");
class email{
var $smtpserver;// SMTP服务器
var $smtpserverport;//SMTP服务器端口
var $smtpusermail;//SMTP服务器的用户邮箱
var $smtpuser;//SMTP服务器的用户帐号
var $smtppass;// SMTP服务器的用户密
var $smtp;
function email($mysmtpserver,$mysmtpserverport=25,$mysmtpusermail,$mysmtpuser,$mysmtppass)
{
$this->smtpserver=$mysmtpserver;
$this->smtpserverport=$mysmtpserverport;
$this->smtpuser=$mysmtpuser;
$this->smtppass=$mysmtppass;
$this->smtpusermail=$mysmtpusermail;
}
function send($mailsubject,$mailbody,$mailtype = "HTML",$smtpemailto)
{
$this->smtp = new smtp($this->smtpserver,$this->smtpserverport,true,$this->smtpuser,$this->smtppass);
$this->smtp->debug = false;
if($this->smtp->sendmail($smtpemailto,$this->smtpusermail, $mailsubject, $mailbody, $mailtype))
{
return true;
}
else {
return false;
}
}
}
复制代码 使用时function send($mailsubject,$mailbody,$smtpemailto,$type)
{
require("lib/email.php");//邮件类
require("APP/Config/email.php");//配置文件
$email=new email($smtpserver,25,$smtpusermail,$smtpuser,$smtppass);
if($email->send($mailsubject,$mailbody,"HTML",$smtpemailto))
return true;
else
return false;
}
然后直接调用send函数就可以发送邮件了
复制代码 smarty
smarty使用时需要配置参数,聪明的方法是建一个类,继承smarty,如下require('Smarty.class.php');
class MySmarty extends Smarty {
function MySmarty() {
$this->Smarty();
$this->template_dir = 'templates/';
$this->compile_dir = 'templates_c/';
$this->config_dir = 'configs/';
$this->cache_dir = '_cache/';
$this->caching = true;
//$this->assign('name','my name');
}
}
//使用时
$smarty = new MySmarty;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
复制代码 [ 本帖最后由 04007147 于 2008-6-10 20:56 编辑 ] |