Home > Article > Backend Development > How to inherit an application in php?
I am trying to write a blog system by myself. When browsing articles, there are some operations that only the author has permission to perform, such as deleting, editing and updating articles. I thought of inheritance to solve it. I published a session class before. Now it is much simpler. Set the user level by logging in. $session->get_status() If the returned value is 0, it means that the current user is not the blogger, and therefore does not have the permission to delete or edit articles. If the return value is 1, it indicates that it is the blogger himself. Okay
, stop talking nonsense. Let’s start with the code
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(); } } }
LimitFactory is a factory class and also a static class. That is, there is no need to construct an object. Its responsibility is only to determine whether to return an instance of the operationLimit class or 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 some new methods, such as returning to delete and updating links.
Usage example
$limitObj = LimitFactory::getLimitObject($session->get_status()); echo $limitObj->getReadA('hi'); echo $limitObj->getDelectA('hah');
Let’s talk about something unrelated. At first, when I didn’t write the getObject() static method in the operationUnlimit class, I found that calling
return operationUnlimit::getObject();
What is returned is an object of the super class, which feels strange. I use self in the getObject(); method to represent the current class, and there is no indication that the object of the super class must be returned. It is only OK when this static method is overridden in the child
class. Later, I checked Google and vaguely understood that the compiler bound the getObject method to the super class at the beginning, so
calls in subclasses still return the superclass object.
Also, if you find it difficult to distinguish so many escape characters in string, then use
echo 47a50357de5e4ef51aae22a5342320cfread more5db79b134e9f6b82c0b36e0489ee08ed
Eeeeeee;
This is a lot more refreshing.
The above is the detailed content of How to inherit an application in php?. For more information, please follow other related articles on the PHP Chinese website!