//ในไฟล์ email.php
require_once(dirname(__FILE__)."/phpmailer/class.phpmailer.php");
class email {
public function send_single($to,$from,$body){
$mail->issmtp();
$mail->.... ว่ากันไปตามวิธีของ phpmailer;
return true;//ส่งเสร็จแล้วก็ return true;
}
}
ใน class เนี้ย ผมไม่อยากจะมา new phpmailer ในทุกๆ method หรือ function อื่นๆใดอีก อยากแบบว่าเขียน function มาใหม่ก็เล่น $mail ได้เลยจะมีวิธีบ้างมั้ยครับ?
<?php
class A{
public function a1(){
echo 'a1;';
}
}
class B{
public $a;
public function __construct(){
$this->a = new A();
}
public function b1(){
$this->a->a1();
}
public function b2(){
$a =& $this->a; //ทำให้เรียกใช้ object a ได้สั้นขึ้น
$a->a1();
}
}
class C extends B{
public function c1(){
$this->a->a1();
}
}
$c = new C();
$c->b1();
$c->b2();
$c->c1();
class dy_Registry
{
public $userinfo;
public $login;
public $db;
public $debug;
public $var;
public $config;
public $lang;
public $cookie;
public $session;
public $forum_cache;
public $module_cache;
public $style_cache;
public $cms_cache;
public $ip_address;
public $host_name;
public function __construct()
{
$this->var =& new dy_Input;
$this->cookie =& new dy_Cookie(COOKIE_PREFIX);
$this->session =& new dy_Session(SESSION_PREFIX);
$this->login =& new dy_Login;
//------------------
// GET USER IPADDRS
//------------------
preg_match( "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/", $this->var->my_get_env('REMOTE_ADDR'), $match );
$this->ip_address = $match[1].'.'.$match[2].'.'.$match[3].'.'.$match[4];
if ( $this->ip_address == '...' )
{
echo "ไม่สามารถทราบหมายเลขไอพีของคุณได้";
exit;
}
$this->host_name = gethostbyaddr($this->ip_address);
}
นั้นแค่ส่วนหนึ่งครับ
อันนี้ class login
ดูดีๆ จะมีการใช้ class หลักมาวน ไป วนมา ใช้ได้สบายๆ เลยครับ
Code (PHP)
class dy_Login
{
public function __construct()
{
}
private function insertLogin($USERID, $USERNAME, $COOKIE = '')
{
global $dyclass;
$LOGINCODE = substr($USERNAME,0,5) . "_" . date("d-m-y_His") . "_" . rand(1111111,9999999);
$dyclass->session->set('USERLOGINCODE', $LOGINCODE);
if ( $COOKIE !== FALSE )
{
$timeArray = array(0 => 365, 1 => 7, 2 => 30, 3 => 90, 4 => 180);
if ( $COOKIE > -1 && $COOKIE <= 4 )
{
$dyclass->cookie->set('USERLOGINCODE', $LOGINCODE, TIME_NOW + 86400 * $timeArray[$COOKIE] );
}
}
$dyclass->db->query_insert('user_login',
array(
'user_id' => $USERID,
'login_code' => $LOGINCODE,
'login_ip' => $dyclass->ip_address,
'login_host' => $this->host_name
)
);
$dyclass->db->query_update('user',
array(
'user_last_login' => date(DATETIME_FORMAT),
'user_total_login' => 'user_total_login+1',
'user_login_status' => 1
),
"user_id = $USERID"
);
}
public function getLogin($USERNAME, $PASSWORD, $COOKIE)
{
global $dyclass;
$sql = $dyclass->db->query_fetch('user', 'user_id, user_name',
'user_name = "' . $db->escape($USERNAME) . '"
AND user_password = "' . md5($db->escape($PASSWORD)) . '"'
);
if ( $dyclass->db->num_rows($sql) == 1 )
{
$row = $dyclass->db->fetch_array($sql);
$this->insertLogin($row['user_id'], $row['user_name'], $COOKIE);
}
return $this->getUserInfo();
}
public function getUserInfo()
{
global $dyclass;
if ( $dyclass->session->get('USERLOGINCODE') )
{
return $this->getUser($dyclass->session->get('USERLOGINCODE'));
}
else if ( $dyclass->cookie->get('USERLOGINCODE') )
{
return $this->getUser($dyclass->cookie->get('USERLOGINCODE'));
}
return $this->getGuest();
}
private function getUser($LOGINCODE)
{
global $dyclass;
$SQL = $db->query("SELECT uID
FROM " . $db->pre ."ulogin
WHERE uLoginCode = '{$LOGINCODE}'
");
if ( $db->numrows($SQL) == 1 )
{
$uLogin = $db->fetch_array($SQL);
$uID = $uLogin['uID'];
$SQL2 = $db->query("SELECT * FROM ". $db->pre ."user, ugroup
WHERE uID=". $uID ."
AND user.uGroupID=ugroup.uGroupID");
if ( $db->numrows($SQL2) == 1 )
{
return $db->fetch_array($SQL2);
}
return $this->getGuest();
}
return $this->getGuest();
}
private function getGuest()
{
global $dyclass;
$sql = $dyclass->db->query_fetch("user_group",'','usergroup_id = 8');
if ( $dyclass->db->num_rows($sql) == 1 )
{
$row = $dyclass->db->fetch_array($sql);
$row['style_id'] = DEFAULT_STYLE_ID;
return $row;
}
return false;
}
public function logOut($USERID, $uLoginCode)
{
...
}
}