Home Backend Development PHP Tutorial How to inherit an application in php?

How to inherit an application in php?

Jun 30, 2017 am 09:18 AM
php application

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles