面向过程与面向对象的简单比较(个人心得)!
时间:2008-04-15 来源:剑心通明
最近打开我以前做的一个项目,系统结构中使用了4个包含文件对登录用户的权限进行判断,属典型的面向过程写法,可能很多朋友以前都写过这样的
[url=javascript:;]代码[/url]
。我把这些代码整理了一下,写成一个权限判断的简单类,以比较一个面各
[url=javascript:;]对象[/url]
和面向过程之间的差异。
代码如下(其中省略了部分代码)。
sesson1.php
session2.php
2 )
{
header("Location: ../index.phtml");
}
?>
session3.php
3 )
{
header("Location: ./right.phtml");
}
?>
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");
}
}
}
?>
调用:
adminPower();
$sessionPower->operatorPower();
$sessionPower->generalPower();
$sessionPower->enterprisePower();
?>
注:如果使用
[url=javascript:;]面向对象[/url]
编程,建议最好使用
[url=javascript:;]zend[/url]
编辑器,这样开发效率会快出很多!
相关阅读 更多 +