比较|对象|过程|心得
最近打开我以前做的一个项目,系统结构中使用了4个包含文件对登录用户的权限进行判断,属典型的面向过程写法,可能很多朋友以前都写过这样的代码。我把这些代码整理了一下,写成一个权限判断的简单类,以比较一个面各对象和面向过程之间的差异。
代码如下(其中省略了部分代码)。
sesson1.php
session2.php
2 )
{
header(location: ../index.phtml);
}
?>
session3.php
3 )
{
header(location: ./right.phtml);
}
?>
session4.php
调用:
include_once(/lib/session1.php);
include_once(/lib/session2.php);
include_once(/lib/session3.php);
include_once(/lib/session4.php);
?>
合并后的权限判断类:
sessionpower.php
username = $http_cookie_vars['username'];//用户名
$this->level = $http_cookie_vars['level'];//权限级别
if ( $this->username == || $this->level == )
{
header(location: ../index.phtml);
}
}
/**
* 是否具有系统管理员权限
*/
function adminpower()
{
if ( $http_cookie_vars['level'] != 1 )
{
header(location: ../right.phtml);
}
}
/**
* 是否具有操作员权限
*/
function operatorpower()
{
if ( $this->level > 2 )
{
header(location: ../index.phtml);
}
}
/**
* 是否具有普通用户权限
*/
function generalpower()
{
if ( $this->level > 3 )
{
header(location: ./right.phtml);
}
}
/**
* 用户是否具有企业用户权限
*/
function enterprisepower()
{
if ( $this->level != 4 )
{
#header(location: ../client_login.phtml);
}
}
}
?>
调用:
include_once(/lib/sessionpower.php);
$sessionpower = new sessionpower();
$sessionpower->adminpower();
$sessionpower->operatorpower();
$sessionpower->generalpower();
$sessionpower->enterprisepower();
?>
注:如果使用面向对象编程,建议最好使用zend编辑器,这样开发效率会快出很多!