How to inherit an application in php?

伊谢尔伦
Release: 2023-03-12 06:40:02
Original
1044 people have browsed it

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 &#39;<a herf=\&#39;http://foodstory.me/post/&#39;.$postname. 
&#39;.php\&#39; class=\&#39;readmoreLink\&#39;>readmore</a>&#39;; 
} 
} 
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 &#39;<a href=\&#39;http://foodstory.me/post/&#39;.$name. 
&#39;.php?do=update\&#39; id=\&#39;&#39;.$name.&#39;\&#39; >update</a>&#39;; 
} 
public function getDelectA($name) 
{ 
return &#39;<a href=\&#39;
javascript
:delectPOST(&#39;.$name 
.&#39;);\&#39; id=\&#39;delectPOST\&#39; >delect</a>&#39;; 
} 
} 
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(); 
} 
} 
}
Copy after login

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(&#39;hi&#39;); 
echo $limitObj->getDelectA(&#39;hah&#39;);
Copy after login

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 <<read more
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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!