I am new to PHP. I am working on a project recently, using ThinkPHP. I want to learn more deeply, so I deliberately studied the source code of ThinkPHP and took notes to record these easily forgotten things. Without further ado, let’s get started.
Official website description:
I method is a new member of ThinkPHP’s many single-letter functions. Its name comes from the English Input (input). It is mainly used to obtain system input variables more conveniently and safely. It can be used Anywhere, the usage format is as follows:
I('Variable type.Variable name',['Default value'],['Filter method'])
Variable type refers to the request method or input type, including:
Variable type | Meaning |
---|---|
get | Get GET parameters |
post | Get POST parameters |
param | Automatically determine the request type Get GET, POST or PUT parameters |
request | get REQUEST parameter |
put | get PUT parameter |
session | get $_SESSION parameter |
cookie | get$_ COOKIE PARAMETERS |
server | GET$ _SERVER parameter |
globals | Get $GLOBALS parameter |
The official code is as follows:Note: Variable types are not case-sensitive.
Variable names are strictly case-sensitive.
Default value and filtering method are optional parameters.
function I($name,$default='',$filter=null,$datas=null) {
static $_PUT=null;//use static to define a static and declare class members Or if the method is static, it can be accessed directly without instantiating the class. Static members cannot be accessed through an object (except static methods)
if(strpos($name,'/')){ // Specify the modifier strpos() function to find a string in another character The first occurrence position in the string, find the first occurrence position of '/' in parameter nam
list($name,$type) =explode('/',$name,2 );//explode(separator,string,limit) function meaning is to spread the string into an array based on specific characters, limit is the number of returned arrays
}elseif(C('VAR_AUTO_STRING')){ // Forced to string by default // Called ThinkPHP's C method
$type = 's';
}
//Summary The meaning of this if is to determine whether the parameter contains /
if(strpos($name,'.')) { //Specify the parameter source //Check whether it contains.!M List ($ Method, $ name) = Explode ('.', $ Name, 2); } Else {// The default is automatically judged
$ method = 'param'; )) {
//strtolower() Convert all characters to lowercase and use switch to locate the type of the method
case 'get' post' :
parse_str(file_get_contents('php://input'), $ _PUT);
switch($_SERVER['REQUEST_METHOD']) { //$_SERVER[' REQUEST_METHOD' Get the requested method name and use swith() to locate the method type. The idea here is recursion
case 'POST': ’ ’ s ’ s ’ ’ s ’ s ’ s ‐ ‐ ‐ ‐ to break; case 'PUT' :
)
case 'path' : $input = array(); if(!empty($_SERVER['PATH_INFO'])){
$ depr = C('URL_PATHINFO_DEPR');
$input = explode($depr,trim($_SERVER['PATH_INFO'],$depr)); break; case 'request' :
$input = & $_REQUEST; break;
case 'session' : $input =& $_SESSION; break;
case ' cookie' : $input =& $_COOKIE; break ;
case 'server' put =& $GLOBALS; break;
case 'data' :
$input =& $datas;
break;
default:
return null;
}
if(''==$name) { // Get all variables
$data = $input;
$filters = isset($filter)?$filter:C('DEFAULT_FILTER');//Used trinocular The operator isset() function is generally used to detect whether a variable is set, and empty() is used to determine whether it is empty. $ Filters); FOREACH () is the cycle function
}}
}elseif(isset($input[$name])) { // Value operation
$data ; If ($ Filters) {
IF (IS_STRING ($ Filters)) {
IF (0 === Strpos ($ FILTERS, '/')) {
if (1! == Preg_match ($ Filters, (String )$data)){ //preg_match() is used to match regular expressions
',',$filters); $filter){
}else{
data = filter_var($data,is_int($filter) ? $filter : filter_id($filter));
}
$data
=
(array)$data; $data; break;
case 'f':// Floating point
// Boolean
$data =(boolean)$data;
break; data = (string)$data; } }
}else { // Variable default value $data using using use using using ’ ‐ ‐ ‐ together using ’ ’ s ’ through ’s ’s to ensue d-- ‐ ‐ The I method is mainly used to obtain the value passed from the front to the back. The method defines four parameters, only one of which is required. This parameter is the name to be obtained, and the others can be changed.
The ones marked in red above are some knowledge points that you should remember when reading the code. Function: Safely obtain parameters passed by inputImplementation idea:
If the type is followed when submitting, it will be processed according to the specified type. If it is not kept up with the specified type, the type will be determined based on the server variable. Ensure the security of submitted data through filtering. Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the method of learning ThinkPHP source code, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
