正在尝试自己写一个博客系统,想当浏览文章的时候,有些操作是只有作者才有权限进行,比如删除,编辑更新文章。
这样我 想到了继承来解决,前面发表过一个会话类,这下简单多了,通过登录来设置用户的级别,$session->get_status()的返回 值如果是0,则表明当前用户并不是博主,因此并不具有删除,编辑文章的权限。如果是返回值是1,则表明是博主本人。好 了,废话少扯。先上代码 复制代码 代码如下: class operationLimit // operating limit. When no user login or is not this user { /* for limit the user operat at post. * @author:xiaoai 8.12 2011 */ static $limitObject; public function __construct() {} // when call the function but does not exist public static function getObject() { if( !(self::$limitObject instanceof self)) self::$limitObject = new self; return self::$limitObject ; } protected function setLimit() {} public function getReadA($postName) { return '<a herf=\'http://foodstory.me/post/'.$postname. '.php\' class=\'readmoreLink\'>readmore</a>'; } } class operationUnlimit extends operationLimit // when is this user { public static function getObject() { if( !(self::$limitObject instanceof self)) self::$limitObject = new self; return self::$limitObject ; } public function getUpdateA($name) { return '<a href=\'http://foodstory.me/post/'.$name. '.php?do=update\' id=\''.$name.'\' >update</a>'; } public function getDelectA($name) { return '<a href=\'javascript:delectPOST('.$name .');\' id=\'delectPOST\' >delect</a>'; } } class LimitFactory { public static function getLimitObject($userStatus) // $userStatus = $session->get_status(); { switch ( $userStatus ) { case 0: return operationLimit::getObject(); case 1: return operationUnlimit::getObject(); default: return limit::getObject(); } } }