class_core.php中 global $_G含意 第一次为了声明这是全局变量 第二次表示传递变量,而不是声明全局
class_core.php中 global $_G含义 第一次为了声明这是全局变量 第二次表示传递变量,而不是声明全局
源:http://www.discuz.net/forum.php?mod=viewthread&tid=3007341
评:global $_G含义 第一次为了声明这是全局变量 第二次表示传递变量,而不是声明全局
看你纠结的,一看就是没怎么做过DZ插件了,这个变量这么用的意义,我这么解释吧:
/source/class/discuz/discuz_application.php(X2.5)
/source/class/class_core.php(X2.0)
在这个文件的_init_env()方法中global $_G的含义就是为了声明这是个全局变量,里面存放的是一些系统最频繁用到的相关的全局信息,主要是为了后面对各功能实现的便利性。例如我要得知当前用户的UID,那我直接读取$_G['uid']就行了,而不用再二次查询session或者数据库,这样可以提高效率,编写其余的各模块程序,代码量也会变得非常简洁。
如果你要问哪里是定义,就是这段(省略一大段):
global $_G; //为了声明这是全局变量
$_G = array(
'uid' => 0,
'username' => '',
..................
);
在除了这个文件以外的其它文件,如果用了global $_G,则表示为了传递这个变量。
例如说我在插件开发中,有这样的方法:
function viewthread_postfooter_output(){
global $_G; //表示传递变量,而不是声明全局
return $_G['uid'];
}
对开发者来说$_G变量的存在意义是“只读”的,换句话说,没有人会写程序的时候,随便去初始化这个变量。
另外作为商业机构而不是公益科研机构的程序作品,工作人员也没有义务来告诉你代码含义吧,所谓的可读性还请自己多加学习理解了?这好比没有哪家饭店主动把招牌菜的烹饪秘方主动告诉顾客一样。

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
