PHP中的魔术方法
来源:互联网 发布:数据洞察 编辑:程序博客网 时间:2024/04/30 04:36
PHP中有下列称之为魔术方法(magic method)的函数:__construct, __destruct ,__call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup,__toString, __set_state, __clone and __autoload,本文使用__call为实现一个身份验证的简单实例,代码如下:
代码
<?php
interface Accountable
{
const ERR_MSG = "error";
public function isLoggedIn();
public function getAccount($user = '');
}
abstract class Authentication implements Accountable
{
private $account = null;
public function getAccount($user = '')
{
if ($this->account != null) {
return $this->account;
} else {
return ERR_MSG;
}
}
public function isLoggedIn()
{
return ($this->account != null);
}
}
class Users
{
private static $accounts = array('phinecos' => 'phine',
'guest' => 'guest'
);
public static function validates($user, $passwd)
{
return self::$accounts[$user] == $passwd;
}
public function __call($name, array $arguments)
{
if (preg_match("/^validates(.*)$/", $name, $matches) && count($arguments) > 0) {
return self::validates($matches[1], $arguments[0]);
}
}
}
class MyAuth extends Authentication
{
private $users;
public function __construct()
{
$this->users = new Users();
}
public function login($user, $passwd)
{
if (empty($user) || empty($passwd)) return false;
$firstValidation = Users::validates($user, $passwd);
$userFunction = 'validates'.$user;
$secondValidation = $this->users->$userFunction($passwd);
return ($firstValidation && $secondValidation);
}
}
function main()
{
$authenticator = new MyAuth();
$user = 'phinecos';
$pwd = 'phine';
$isValid = $authenticator->login($user, $pwd);
if ($isValid) {
echo 'valid user';
} else {
echo 'invalid user';
}
}
main();
?>
interface Accountable
{
const ERR_MSG = "error";
public function isLoggedIn();
public function getAccount($user = '');
}
abstract class Authentication implements Accountable
{
private $account = null;
public function getAccount($user = '')
{
if ($this->account != null) {
return $this->account;
} else {
return ERR_MSG;
}
}
public function isLoggedIn()
{
return ($this->account != null);
}
}
class Users
{
private static $accounts = array('phinecos' => 'phine',
'guest' => 'guest'
);
public static function validates($user, $passwd)
{
return self::$accounts[$user] == $passwd;
}
public function __call($name, array $arguments)
{
if (preg_match("/^validates(.*)$/", $name, $matches) && count($arguments) > 0) {
return self::validates($matches[1], $arguments[0]);
}
}
}
class MyAuth extends Authentication
{
private $users;
public function __construct()
{
$this->users = new Users();
}
public function login($user, $passwd)
{
if (empty($user) || empty($passwd)) return false;
$firstValidation = Users::validates($user, $passwd);
$userFunction = 'validates'.$user;
$secondValidation = $this->users->$userFunction($passwd);
return ($firstValidation && $secondValidation);
}
}
function main()
{
$authenticator = new MyAuth();
$user = 'phinecos';
$pwd = 'phine';
$isValid = $authenticator->login($user, $pwd);
if ($isValid) {
echo 'valid user';
} else {
echo 'invalid user';
}
}
main();
?>
- PHP中的魔术方法
- PHP中的魔术方法
- php中的魔术方法
- php中的魔术方法
- PHP中的魔术方法
- php中的魔术方法
- php中的魔术方法
- php中的魔术方法
- php中的魔术方法
- PHP中的魔术方法
- PHP中的魔术方法
- PHP 中的魔术方法
- php中的魔术方法
- PHP中的魔术方法
- PHP中的魔术方法
- php中的魔术方法
- PHP中的魔术方法
- PHP中的魔术常量与魔术方法
- 迁移、重构项目的注意事项
- iomanip头文件常用函数汇总
- 绑定水晶报表的两种方式Pull和Push[vs2008环境]
- xenserver CIFS&&NFS
- LINQ 学习笔记之存储过程!
- PHP中的魔术方法
- xenserver新增试验网卡
- GDE-X进展 战场框架
- Oracle集合类型输出参数的PL/SQL存储过程及其Java调用
- 【表达式运算】hdu hdoj 3000 A Simple Language
- 什么是LVM
- LVM工具
- LVM mapping
- LVM snapshot