In this way, I
I thought of inheritance to solve the problem. I have previously published a session class, which is much simpler. I set the user level through login, and the return of $ session-> get_status ()
If the value is 0, it indicates that the current user is not a blogger and therefore does not have the permission to delete or edit an article. If the return value is 1, it indicates that it is the blogger himself. Good
No. Code first
Copy codeThe Code is as follows:
Class operationLimit
// Operating limit. When no user login or is not this user
{
/* For limit the user operat 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 ();
}
}
}
LimitFactory is a factory class and static class. That is, you do not need to construct an object. Its role is to judge whether the returned operationLimit class or operationUnlimit class instance is based on the passed user permission value.
There are some common operations, such as reading more, the operationUnlimit class inherits this method, and then creates some new methods, such as return deletion and update link methods.
Usage example
Copy codeThe Code is as follows:
$ LimitObj = LimitFactory: getLimitObject ($ session-> get_status ());
Echo $ limitObj-> getReadA ('Hi ');
Echo $ limitObj-> getDelectA ('hah ');
At the beginning, when I did not write the getObject () Static Method in the operationUnlimit class, I found that
Return operationUnlimit: getObject ();
The returned result is a superclass object. It is strange that I use self in the getObject (); Method to represent the current class. It does not indicate that the superclass object must be returned. When
Class. Later, I checked google and vaguely understood that the compiler bound the getObject method and the superclass at the beginning, so
The call in the subclass still returns the superclass object.
Also, do you think it is difficult to distinguish so many escape characters in the string?
Echo <Eeeeeee
<A href = 'foodstory. me/post/{$ name}. php'> read more </a>
Eeeeeee;
This is refreshing.