


Explanation of the difference between _initialize() function and __construct() function in PHP
This article brings you an explanation of the difference between the _initialize() function and the __construct() function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
_initialize() method must be executed before any method is executed, including of course __construct constructor. That is to say, if there is a _initialize() function, calling any method of the object will cause the _initialize() function to be automatically called, and the __construct() constructor is only called once when the object is created, and has nothing to do with other method calls.
__construct here is a double dash, and the _initialize() function is a single dash
If both the parent and child classes have the _initialize() function, the child class overrides the parent class. If If the subclass does not have it but the parent class does, the subclass inherits it from the parent class.
By default, the constructor of the subclass will not automatically call the constructor of the parent class. When calling _initialize() of a subclass object, it will not automatically call _initialize() of the parent class.
When actually writing the constructor of the subclass, you generally need to add the constructor of the parent class. Actively call parent::__construct(), otherwise it will cause a null pointer exception of the subclass object, such as Call to a member function assign() on a non-object.
Therefore, in some system background management or comment functions, the relevant controller can be extended to the base controller:
Base.php:
<?php namespace app\admin\controller; use think\Controller; class Base extends Controller { public function _initialize(){ if(!session('username')){ $this->error('请先登录系统!','Login/index'); } } }
Article.php
<?php namespace app\Admin\controller; use app\Admin\model\Article as ArticleModel; use app\admin\controller\Base; class Article extends Base { ……… ……… }
In this way, when any method of the Article controller is executed, the _initialize() method in Base.php will be executed first to detect whether you have logged in, without creating an object.
Related recommendations:
The difference between the explode() function and strtok() function in php
##method_exists( in php ) and is_callable() function analysis
The above is the detailed content of Explanation of the difference between _initialize() function and __construct() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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


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

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.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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