Home > Backend Development > PHP Tutorial > An application inherited from php_PHP tutorial

An application inherited from php_PHP tutorial

WBOY
Release: 2016-07-21 15:24:38
Original
835 people have browsed it

In this way, I
thought of inheritance to solve the problem. I published a session class before, but it is much simpler now. Set the user level by logging in. If the return
value of $session->get_status() is 0, It means that the current user is not the blogger, so he 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. First enter the code

Copy the code The code is as follows:

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 ' '.php' class='readmoreLink'>readmore';
}
}
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 ''.php?do=update' id=''.$name.'' >update';
}
public function getDelectA($name)
{
return '.');' id='delectPOST' >delect';
}
}
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 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
Copy code The code is as follows:

$limitObj = LimitFactory::getLimitObject($session-> get_status());
echo $limitObj->getReadA('hi');
echo $limitObj->getDelectA('hah');

The following point is irrelevant Yes, when I first didn’t write the getObject() static method in the operationUnlimit class, I found that calling
return operationUnlimit::getObject();
returned an object of the super class, which felt strange. ); The method uses self to represent the current class, and does not specify that the object of the super class must be returned. It is only OK when this static method is overridden in the sub-
class. Later, I checked Google, and I 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 the string, then use
echo <<
read more
Eeeeeee;
This is a lot more refreshing

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324294.htmlTechArticleIn this way, I thought of inheritance to solve the problem. I published a session class before, but it is much simpler now. You can do it by logging in. Set the user level. If the return value of $session-get_status() is 0, the table...
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