so that I
Think of inheritance to solve, previously published a session class, which is much simpler, through login to set the level of users, $session->get_status () of the return
A value of 0 indicates that the current user is not a blogger and therefore does not have permission to delete and edit the article. If the return value is 1, then the blogger himself is indicated. Good
, cut the crap. First up code
Copy Code code as follows:
Class Operationlimit
Operating limit. When no user login or was 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 was 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, also a static class. That is, there is no need to construct the object, its responsibility is to determine whether to return the Operationlimit class or an instance of the Operationunlimit class based on the incoming user permission value.
There are some common operations, such as reading more, the Operationunlimit class inherits this method, and then creates new methods, such as returning deletes and updating links.
Usage examples
Copy Code code as follows:
$LIMITOBJ = Limitfactory::getlimitobject ($session->get_status ());
echo $limitObj->getreada (' Hi ');
echo $limitObj->getdelecta (' hah ');
Here's a little bit irrelevant, just beginning when I didn't write the GetObject () static method in the Operationunlimit class, the discovery called
return Operationunlimit::getobject ();
The object that returns is the superclass, it feels strange, I am GetObject (), the method uses self to represent the current class, and does not indicate the object that must return the superclass. When in the child
This static method is overridden in a class to be OK. Later looked at Google, vaguely understand that the compiler at the beginning of the GetObject method and super class binding together, so
Calls in subclasses are still returning superclass objects.
Also, if you feel that there are so many escape characters in the string that are hard to distinguish, switch to
Echo <<<eeeeeee
<a href= ' foodstory.me/post/{$name}.php ' >read more</a>
eeeeeee;
It's a lot more refreshing.