Home php教程 php手册 面向过程与面向对象的简单比较(个人心得)!

面向过程与面向对象的简单比较(个人心得)!

Jun 21, 2016 am 09:11 AM
cookie nbsp quot

比较|对象|过程|心得

最近打开我以前做的一个项目,系统结构中使用了4个包含文件对登录用户的权限进行判断,属典型的面向过程写法,可能很多朋友以前都写过这样的代码。我把这些代码整理了一下,写成一个权限判断的简单类,以比较一个面各对象和面向过程之间的差异。
代码如下(其中省略了部分代码)。

sesson1.php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,并是否具有系统管理员权限
* 程序员:xiangli
* 日期:2002-07-19
*/

$UserName = $HTTP_COOKIE_VARS['UserName1'];//用户名
if ( empty($UserName) || $HTTP_COOKIE_VARS['Level'] != 1 )
{
    header("Location: ../right.phtml");
}
?>

session2.php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,并是否具有操作员权限
* 程序员:xiangli
* 日期:2001-07-19
*/

$UserName = $HTTP_COOKIE_VARS['UserName1'];//用户名
$Level = $HTTP_COOKIE_VARS['Level'];//权限级别

if ( empty($UserName) || $Level > 2 )
{
    header("Location: ../index.phtml");
}
?>

session3.php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,用户是否具有普通用户权限
* 程序员:xiangli
* 日期:2001-07-19
*/

if ( empty($UserName1) || $Level > 3 )
{
    header("Location: ./right.phtml");
}
?>

session4.php
/*
* 功能:取得用户的cookie,以判断用户是否已经登录,用户是否具有企业用户权限
* 程序员:xiangli
* 日期:2001-08-11
*/

if ( empty($_COOKIE['ClientName']) || $_COOKIE['Level'] != 4 )
{
    #header("Location: ../client_login.phtml");
}
?>

调用:

include_once("/lib/session1.php");
include_once("/lib/session2.php");
include_once("/lib/session3.php");
include_once("/lib/session4.php");
?>

合并后的权限判断类:
sessionPower.php
/**
* @功能:根据cookie的值判断用户是否已经登录及用户的权限
* @程序员:xiangli
* @日期:2002-12-20
*/

class sessionPower{
    var Username;//用户名
    var Level;//用户权力级别
    
    /**
    * 判断用户是否已经登录
    */
    function sessionPower()
    {
        $this->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编辑器,这样开发效率会快出很多!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

How to change title bar color on Windows 11?

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

How to enable or disable taskbar thumbnail previews on Windows 11

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

Display scaling guide on Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

How to turn off private browsing authentication for iPhone in Safari?

Win10/11 digital activation script MAS version 2.2 re-supports digital activation Win10/11 digital activation script MAS version 2.2 re-supports digital activation Oct 16, 2023 am 08:13 AM

Win10/11 digital activation script MAS version 2.2 re-supports digital activation

See all articles