Home Backend Development PHP Tutorial A simple template engine class. This type of research is not perfect. I hope friends can participate in the study and research.

A simple template engine class. This type of research is not perfect. I hope friends can participate in the study and research.

Jul 25, 2016 am 09:01 AM

This is the first time I post code here. This code is mainly for PHP template engine technology research. Currently there is only a compiled version. I hope you can provide more opinions and optimization tips.
It consists of three files. I don’t know how to format it as a file, so I can only copy it. Sorry!
index.php is a configuration file, everyone will understand after taking a look
index.html Some usage examples
Templates.class.php base class
A perfect version of caching will be released later, but I hope it will not be cached. With the guidance of friends or experts, this template engine only needs to handle compilation and caching, and other considerations will not be considered for the time being. , of course, the regular replacement mode also needs to add f, w and so on. . .
I hope some friends can study my Q:
76376931

Copy_3_of_Templates.class.php file has added a cache method. Refreshing the page again will not generate a cache. It has not been considered whether some pages in the project need to be cached. This class will be added gradually in the future. I hope some friends can join me. comminicate!
  1. header('Content-Type:text/html;charset=utf-8');
  2. define('ROOT_HOST',dirname(__FILE__));
  3. define('HTML_DIR',ROOT_HOST. '/moban/');
  4. define('COMPILED_DIR',ROOT_HOST.'/data/compiled/');
  5. define('CACHE_DIR',ROOT_HOST.'/data/cache/');
  6. //Whether to enable the buffer
  7. define('NEW_CACHE', false);
  8. //Determine whether to open the buffer
  9. NEW_CACHE? ob_start(): null;
  10. //Introduce the template class
  11. require ROOT_HOST.'/lib/Templates.class.php';
  12. $_moban = new Templates();
  13. $array = array(a=>'Hello',b=>'I'm not very good, but I miss you very much',c=>'You are all here You're at home, why do you still miss me?');
  14. $xcvu = 'Hello, this is an XCVU';
  15. $zmq = "hi";
  16. $title = "This is a template engine customization method!" ;
  17. $ling = "Because a "function" is being modified????????????????";
  18. $_moban->assign('ling', $ling);
  19. $ _moban->assign('title',$title);
  20. $_moban->assign('zmq', $zmq);
  21. $_moban->assign('xcvu', $xcvu);
  22. $_moban- >assign('abc',5>4);
  23. $_moban->assign('array', $array);
  24. $_moban->display('index.html');
  25. ?>
Copy code
  1. <!-- $title -->< ;/title></li> <li><style type="text/css"></li> <li>*{ margin:0; padding:0;}</li> <li>body{ font-size:12px; color:#fff; background:#999;}</li> <li> .index { margin:0 auto; width:960px; background:#fff;height:1000px; line-height:50px; padding:20px;color:#000;}</li> <li>.index a{color:#000; text-decoration :none;}</li> <li>.index a:hover{ color:#F0F;}</li> <li></style></li> <li></head></li> <li><body></li> <li><div class="index"></li> <li> <span style ="color:#000;">BBBasd</span><span style="color:#000;">I don’t know what to say, but I want to say something</span>< br /></li> <li> <a href="#"><!-- $ling --></a></li> <li> <br /></li> <li> <!-- $xcvu --> </li> <li> <br /></li> <li> <!-- if $abc --></li> <li> <P>No.1</P></li> <li> <!-- else --></li> <li> <P>No.2 </P></li> <li> <!-- /if --></li> <li> <br /></li> <li> <!-- loop $array(k,v) --></li> <li> <!-- @k -->........<!-- @v --><br/></li> <li> <!-- /loop --></li> <li> <!-- #This is PHP comments --></li> <li></div></li> <li></body></li> <li></html></li> </ol></div> <em onclick="copycode($('code_NP9'));">Copy code</em> </div> <div class="blockcode"> <div id="code_zuF"><ol> <li><?php<li>/* about:Richard.z<li> * site:http://www.zmq.cc<li> * E_mail:code@zmq.cc<li> * date:2013/01/02/17:30<li> * */<li>class Templates{<li> private $_CaChe;<li> private $_Compiled;<li> private $_HtmlFile;<li> private $_FileVar;<li> private $_KeyArr = array();<li> <li> public function __construct(){<li> if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){<li> exit('Your directory does not exist!');<li> }<li> }<li> <li> public function assign($_var, $_value){<li> if(isset($_var) && !empty($_var)){<li> $this->_KeyArr[$_var] = $_value;</li> <li> }else{</li> <li> exit('Please set your value!');</li> <li> } </li> <li> }</li> <li> </li> <li> public function display($_File){</li> <li> //设置模板的变量</li> <li> $this->_HtmlFile = HTML_DIR.$_File;</li> <li> //设置编译</li> <li> $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';</li> <li> //设置缓存</li> <li> $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';</li> <li> //判断模板是否存在</li> <li> if(!file_exists($this->_HtmlFile)){</li> <li> exit('Template file does not exist');</li> <li> }</li> <li> //赋值和判断读取</li> <li> if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){</li> <li> exit('The template file read error!');</li> <li> }</li> <li> //if edit Compiled File date < date HtmlFile <li> if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) < filemtime($this->_HtmlFile)){</li> <li> $this->Set_Comilled();</li> <li> }</li> <li> //Include Compiled</li> <li> include $this->_Compiled; </li> <li> }</li> <li> <li> //public function</li> <li> public function Set_Comilled(){</li> <li> $this->SetArr();</li> <li> $this->SetInclude();</li> <li> if(!file_put_contents($this->_Compiled, $this->_FileVar)){</li> <li> exit('Compiled files generated error!');</li> <li> }</li> <li> }</li> <li> //arr</li> <li> private function SetArr(){</li> <li> $_preaa = array(</li> <li> '/<!--s+$([w]+)s+-->/',</li> <li> '/<!--s+ifs+$([w]+)s+-->/',</li> <li> '/<!--s+/ifs+-->/',</li> <li> '/<!--s+elses+-->/',</li> <li> '/<!--s+loops+$([w]+)(([w]+),([w]+))s+-->/',</li> <li> '/<!--s+/loops+-->/',</li> <li> '/<!--s+@([w]+)s+-->/',</li> <li> '/<!--s+#(.*)s+-->/');</li> <li> $_prebb = array(</li> <li> '<?php echo $this->_KeyArr["$1"];?>',</li> <li> '<?php if ($this->_KeyArr["$1"]) {?>',</li> <li> '<?php } ?>',</li> <li> '<?php } else { ?>',</li> <li> '<?php foreach ($this->_KeyArr["$1"] as $$2=>$$3) { ?>',</li> <li> '<?php } ?>',</li> <li> '<?php echo $$1; ?>',</li> <li> '<?php /* $1 */ ?>');</li> <li> $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);</li> <li> if(preg_match($_preaa[0], $this->_FileVar)){</li> <li> $this->_FileVar = $this->SetArr($this->_FileVar);</li> <li> }</li> <li> }</li> <li> <li> //Include</li> <li> private function SetInclude(){</li> <li> $_preFile = '/<!--s+includes+file="([w.-]+)"s+-->/';</li> <li> if(preg_match($_preFile, $this->_FileVar,$_File)){</li> <li> if(!file_exists($_File[1]) || empty($_File)){</li> <li> exit('You of Include File Error!');</li> <li> }</li> <li> $this->_FileVar = preg_replace($_preFile, "<?php include '$1'; ?>", $this->_FileVar);</li> <li> }</li> <li> }</li> <li> </li> <li>}</li> <li>?></li> </ol></div> <em onclick="copycode($('code_zuF'));">复制代码</em> </div> <div class="blockcode"> <div id="code_pmx"><ol> <li><?php<li>/* about:Richard.z<li> * site:http://www.zmq.cc<li> * E_mail:code@zmq.cc<li> * date:2013/01/02/17:30 || 2013/01/14/21:35<li> * */<li>class Templates{<li> private $_CaChe;<li> private $_Compiled;<li> private $_HtmlFile;<li> private $_FileVar;<li> private $_KeyArr = array();<li> <li> public function __construct(){<li> if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){<li> exit('Your directory does not exist!');<li> }<li> }<li> <li> public function assign($_var, $_value){<li> if(isset($_var) && !empty($_var)){<li> $this->_KeyArr[$_var] = $_value;</li> <li> }else{</li> <li> exit('Please set your value!');</li> <li> } </li> <li> }</li> <li> </li> <li> public function display($_File){</li> <li> //设置模板的变量</li> <li> $this->_HtmlFile = HTML_DIR.$_File;</li> <li> //设置编译</li> <li> $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';</li> <li> //设置缓存</li> <li> $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';</li> <li> //判断模板是否存在</li> <li> if(!file_exists($this->_HtmlFile)){</li> <li> exit('Template file does not exist');</li> <li> }</li> <li> //赋值和判断读取</li> <li> if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){</li> <li> exit('The template file read error!');</li> <li> }</li> <li> //if edit Compiled File date < date HtmlFile <li> if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) < filemtime($this->_HtmlFile)){</li> <li> $this->Set_Comilled();</li> <li> }</li> <li> //Include Compiled</li> <li> include $this->_Compiled;</li> <li> $this->SetCaChe();</li> <li> }</li> <li> <li> //The setting cache file if you want to be generated again</li> <li> private function SetCaChe(){</li> <li> if(!file_exists($this->_CaChe) || filemtime($this->_CaChe) < filemtime($this->_Compiled)){</li> <li> if(NEW_CACHE){</li> <li> file_put_contents($this->_CaChe, ob_get_contents());</li> <li> ob_end_clean();</li> <li> include $this->_CaChe;</li> <li> }</li> <li> }</li> <li> }</li> <li> <li> //public function</li> <li> public function Set_Comilled(){</li> <li> $this->SetArr();</li> <li> $this->SetInclude();</li> <li> if(!file_put_contents($this->_Compiled, $this->_FileVar)){</li> <li> exit('Compiled files generated error!');</li> <li> }</li> <li> }</li> <li> //arr</li> <li> private function SetArr(){</li> <li> $_preaa = array(</li> <li> '/<!--s+$([w]+)s+-->/',</li> <li> '/<!--s+ifs+$([w]+)s+-->/',</li> <li> '/<!--s+/ifs+-->/',</li> <li> '/<!--s+elses+-->/',</li> <li> '/<!--s+loops+$([w]+)(([w]+),([w]+))s+-->/',</li> <li> '/<!--s+/loops+-->/',</li> <li> '/<!--s+@([w]+)s+-->/',</li> <li> '/<!--s+#(.*)s+-->/');</li> <li> $_prebb = array(</li> <li> '<?php echo $this->_KeyArr["$1"];?>',</li> <li> '<?php if ($this->_KeyArr["$1"]) {?>',</li> <li> '<?php } ?>',</li> <li> '<?php } else { ?>',</li> <li> '<?php foreach ($this->_KeyArr["$1"] as $$2=>$$3) { ?>',</li> <li> '<?php } ?>',</li> <li> '<?php echo $$1; ?>',</li> <li> '<?php /* $1 */ ?>');</li> <li> $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);</li> <li> if(preg_match($_preaa[0], $this->_FileVar)){</li> <li> $this->_FileVar = $this->SetArr($this->_FileVar);</li> <li> }</li> <li> }</li> <li> <li> //Include</li> <li> private function SetInclude(){</li> <li> $_preFile = '/<!--s+includes+file="([w.-]+)"s+-->/';</li> <li> if(preg_match($_preFile, $this->_FileVar,$_File)){</li> <li> if(!file_exists($_File[1]) || empty($_File)){</li> <li> exit('You of Include File Error!');</li> <li> }</li> <li> $this->_FileVar = preg_replace($_preFile, "<?php include '$1'; ?>", $this->_FileVar);</li> <li> }</li> <li> }</li> <li> </li> <li>}</li> <li>?></li> </ol></div> <em onclick="copycode($('code_pmx'));">复制代码</em> </div> </td></tr></table> <div id="comment_35146" class="cm"> </div> <div id="post_rate_div_35146"></div> <br><br> </div> </div> <div class="wzconShengming_sp"> <div class="bzsmdiv_sp">Statement of this Website</div> <div>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</div> </div> </div> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="2507867629"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <div class="AI_ToolDetails_main4sR"> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-5902227090019525" data-ad-slot="3653428331" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <!-- <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>Hot Article</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796785841.html" title="Assassin's Creed Shadows: Seashell Riddle Solution" class="phpgenera_Details_mainR4_bottom_title">Assassin's Creed Shadows: Seashell Riddle Solution</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 weeks ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796789525.html" title="What's New in Windows 11 KB5054979 & How to Fix Update Issues" class="phpgenera_Details_mainR4_bottom_title">What's New in Windows 11 KB5054979 & How to Fix Update Issues</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 weeks ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796785857.html" title="Where to find the Crane Control Keycard in Atomfall" class="phpgenera_Details_mainR4_bottom_title">Where to find the Crane Control Keycard in Atomfall</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 weeks ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796783009.html" title="Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation" class="phpgenera_Details_mainR4_bottom_title">Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>1 months ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796784440.html" title="Roblox: Dead Rails - How To Complete Every Challenge" class="phpgenera_Details_mainR4_bottom_title">Roblox: Dead Rails - How To Complete Every Challenge</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 weeks ago</span> <span>By DDD</span> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/article.html">Show More</a> </div> </div> </div> --> <div class="phpgenera_Details_mainR3"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hottools2.png" alt="" /> <h2>Hot AI Tools</h2> </div> <div class="phpgenera_Details_mainR3_bottom"> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411540686492.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undresser.AI Undress" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_title"> <h3>Undresser.AI Undress</h3> </a> <p>AI-powered app for creating realistic nude photos</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411552797167.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="AI Clothes Remover" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_title"> <h3>AI Clothes Remover</h3> </a> <p>Online AI tool for removing clothes from photos.</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173410641626608.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undress AI Tool" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_title"> <h3>Undress AI Tool</h3> </a> <p>Undress images for free</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411529149311.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Clothoff.io" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_title"> <h3>Clothoff.io</h3> </a> <p>AI clothes remover</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/ai_manual/001/246/273/173414504068133.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Video Face Swap" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_title"> <h3>Video Face Swap</h3> </a> <p>Swap faces in any video effortlessly with our completely free AI face swap tool!</p> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/ai">Show More</a> </div> </div> </div> <script src="https://sw.php.cn/hezuo/cac1399ab368127f9b113b14eb3316d0.js" type="text/javascript"></script> <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>Hot Article</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796785841.html" title="Assassin's Creed Shadows: Seashell Riddle Solution" class="phpgenera_Details_mainR4_bottom_title">Assassin's Creed Shadows: Seashell Riddle Solution</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 weeks ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796789525.html" title="What's New in Windows 11 KB5054979 & How to Fix Update Issues" class="phpgenera_Details_mainR4_bottom_title">What's New in Windows 11 KB5054979 & How to Fix Update Issues</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>2 weeks ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796785857.html" title="Where to find the Crane Control Keycard in Atomfall" class="phpgenera_Details_mainR4_bottom_title">Where to find the Crane Control Keycard in Atomfall</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 weeks ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796783009.html" title="Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation" class="phpgenera_Details_mainR4_bottom_title">Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>1 months ago</span> <span>By DDD</span> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/1796784440.html" title="Roblox: Dead Rails - How To Complete Every Challenge" class="phpgenera_Details_mainR4_bottom_title">Roblox: Dead Rails - How To Complete Every Challenge</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <span>3 weeks ago</span> <span>By DDD</span> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/article.html">Show More</a> </div> </div> </div> <div class="phpgenera_Details_mainR3"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hottools2.png" alt="" /> <h2>Hot Tools</h2> </div> <div class="phpgenera_Details_mainR3_bottom"> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/toolset/development-tools/92" title="Notepad++7.3.1" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab96f0f39f7357.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Notepad++7.3.1" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/toolset/development-tools/92" title="Notepad++7.3.1" class="phpmain_tab2_mids_title"> <h3>Notepad++7.3.1</h3> </a> <p>Easy-to-use and free code editor</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/toolset/development-tools/93" title="SublimeText3 Chinese version" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab97a3baad9677.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Chinese version" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/toolset/development-tools/93" title="SublimeText3 Chinese version" class="phpmain_tab2_mids_title"> <h3>SublimeText3 Chinese version</h3> </a> <p>Chinese version, very easy to use</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/toolset/development-tools/121" title="Zend Studio 13.0.1" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58ab97ecd1ab2670.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Zend Studio 13.0.1" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/toolset/development-tools/121" title="Zend Studio 13.0.1" class="phpmain_tab2_mids_title"> <h3>Zend Studio 13.0.1</h3> </a> <p>Powerful PHP integrated development environment</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58d0e0fc74683535.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Dreamweaver CS6" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_title"> <h3>Dreamweaver CS6</h3> </a> <p>Visual web development tools</p> </div> </div> <div class="phpmain_tab2_mids_top"> <a href="https://www.php.cn/toolset/development-tools/500" title="SublimeText3 Mac version" class="phpmain_tab2_mids_top_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" class="lazy" data-src="https://img.php.cn/upload/manual/000/000/001/58d34035e2757995.png?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Mac version" /> </a> <div class="phpmain_tab2_mids_info"> <a href="https://www.php.cn/toolset/development-tools/500" title="SublimeText3 Mac version" class="phpmain_tab2_mids_title"> <h3>SublimeText3 Mac version</h3> </a> <p>God-level code editing software (SublimeText3)</p> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/ai">Show More</a> </div> </div> </div> <div class="phpgenera_Details_mainR4"> <div class="phpmain1_4R_readrank"> <div class="phpmain1_4R_readrank_top"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/hotarticle2.png" alt="" /> <h2>Hot Topics</h2> </div> <div class="phpgenera_Details_mainR4_bottom"> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/gmailyxdlrkzn" title="Where is the login entrance for gmail email?" class="phpgenera_Details_mainR4_bottom_title">Where is the login entrance for gmail email?</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>7612</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>15</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/cakephp-tutor" title="CakePHP Tutorial" class="phpgenera_Details_mainR4_bottom_title">CakePHP Tutorial</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>1387</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>52</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/steamdzhmcssmgs" title="What is the format of the account name of steam" class="phpgenera_Details_mainR4_bottom_title">What is the format of the account name of steam</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>88</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>11</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/winactivationkeyper" title="win11 activation key permanent" class="phpgenera_Details_mainR4_bottom_title">win11 activation key permanent</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>68</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>19</span> </div> </div> </div> <div class="phpgenera_Details_mainR4_bottoms"> <a href="https://www.php.cn/faq/newyorktimesdailybrief" title="nyt connections hints and answers" class="phpgenera_Details_mainR4_bottom_title">nyt connections hints and answers</a> <div class="phpgenera_Details_mainR4_bottoms_info"> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/eyess.png" alt="" /> <span>29</span> </div> <div class="phpgenera_Details_mainR4_bottoms_infos"> <img src="/static/imghw/tiezi.png" alt="" /> <span>136</span> </div> </div> </div> </div> <div class="phpgenera_Details_mainR3_more"> <a href="https://www.php.cn/faq/zt">Show More</a> </div> </div> </div> </div> </div> <div class="Article_Details_main2"> <div class="phpgenera_Details_mainL4"> <div class="phpmain1_2_top"> <a href="javascript:void(0);" class="phpmain1_2_top_title">Related knowledge<img src="/static/imghw/index2_title2.png" alt="" /></a> </div> <div class="phpgenera_Details_mainL4_info"> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796787277.html" title="Alipay PHP SDK transfer error: How to solve the problem of 'Cannot declare class SignData'?" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/246/273/174303625625009.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Alipay PHP SDK transfer error: How to solve the problem of 'Cannot declare class SignData'?" /> </a> <a href="https://www.php.cn/faq/1796787277.html" title="Alipay PHP SDK transfer error: How to solve the problem of 'Cannot declare class SignData'?" class="phphistorical_Version2_mids_title">Alipay PHP SDK transfer error: How to solve the problem of 'Cannot declare class SignData'?</a> <span class="Articlelist_txts_time">Apr 01, 2025 am 07:21 AM</span> <p class="Articlelist_txts_p">Alipay PHP...</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796790404.html" title="Explain JSON Web Tokens (JWT) and their use case in PHP APIs." class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174378264165720.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Explain JSON Web Tokens (JWT) and their use case in PHP APIs." /> </a> <a href="https://www.php.cn/faq/1796790404.html" title="Explain JSON Web Tokens (JWT) and their use case in PHP APIs." class="phphistorical_Version2_mids_title">Explain JSON Web Tokens (JWT) and their use case in PHP APIs.</a> <span class="Articlelist_txts_time">Apr 05, 2025 am 12:04 AM</span> <p class="Articlelist_txts_p">JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796782794.html" title="Explain the concept of late static binding in PHP." class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/202503/21/2025032113332596253.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Explain the concept of late static binding in PHP." /> </a> <a href="https://www.php.cn/faq/1796782794.html" title="Explain the concept of late static binding in PHP." class="phphistorical_Version2_mids_title">Explain the concept of late static binding in PHP.</a> <span class="Articlelist_txts_time">Mar 21, 2025 pm 01:33 PM</span> <p class="Articlelist_txts_p">Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796791139.html" title="How does session hijacking work and how can you mitigate it in PHP?" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174386897193010.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How does session hijacking work and how can you mitigate it in PHP?" /> </a> <a href="https://www.php.cn/faq/1796791139.html" title="How does session hijacking work and how can you mitigate it in PHP?" class="phphistorical_Version2_mids_title">How does session hijacking work and how can you mitigate it in PHP?</a> <span class="Articlelist_txts_time">Apr 06, 2025 am 12:02 AM</span> <p class="Articlelist_txts_p">Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796788900.html" title="Describe the SOLID principles and how they apply to PHP development." class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174360984159295.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Describe the SOLID principles and how they apply to PHP development." /> </a> <a href="https://www.php.cn/faq/1796788900.html" title="Describe the SOLID principles and how they apply to PHP development." class="phphistorical_Version2_mids_title">Describe the SOLID principles and how they apply to PHP development.</a> <span class="Articlelist_txts_time">Apr 03, 2025 am 12:04 AM</span> <p class="Articlelist_txts_p">The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796786988.html" title="How to automatically set permissions of unixsocket after system restart?" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/246/273/174304058392432.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How to automatically set permissions of unixsocket after system restart?" /> </a> <a href="https://www.php.cn/faq/1796786988.html" title="How to automatically set permissions of unixsocket after system restart?" class="phphistorical_Version2_mids_title">How to automatically set permissions of unixsocket after system restart?</a> <span class="Articlelist_txts_time">Mar 31, 2025 pm 11:54 PM</span> <p class="Articlelist_txts_p">How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796787531.html" title="How to debug CLI mode in PHPStorm?" class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/246/273/174277501469931.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="How to debug CLI mode in PHPStorm?" /> </a> <a href="https://www.php.cn/faq/1796787531.html" title="How to debug CLI mode in PHPStorm?" class="phphistorical_Version2_mids_title">How to debug CLI mode in PHPStorm?</a> <span class="Articlelist_txts_time">Apr 01, 2025 pm 02:57 PM</span> <p class="Articlelist_txts_p">How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...</p> </div> <div class="phphistorical_Version2_mids"> <a href="https://www.php.cn/faq/1796788902.html" title="Explain late static binding in PHP (static::)." class="phphistorical_Version2_mids_img"> <img onerror="this.onerror=''; this.src='/static/imghw/default1.png'" src="/static/imghw/default1.png" class="lazy" data-src="https://img.php.cn/upload/article/001/253/068/174360989012815.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="Explain late static binding in PHP (static::)." /> </a> <a href="https://www.php.cn/faq/1796788902.html" title="Explain late static binding in PHP (static::)." class="phphistorical_Version2_mids_title">Explain late static binding in PHP (static::).</a> <span class="Articlelist_txts_time">Apr 03, 2025 am 12:04 AM</span> <p class="Articlelist_txts_p">Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.</p> </div> </div> <a href="https://www.php.cn/be/" class="phpgenera_Details_mainL4_botton"> <span>See all articles</span> <img src="/static/imghw/down_right.png" alt="" /> </a> </div> </div> </div> </main> <footer> <div class="footer"> <div class="footertop"> <img src="/static/imghw/logo.png" alt=""> <p>Public welfare online PHP training,Help PHP learners grow quickly!</p> </div> <div class="footermid"> <a href="https://www.php.cn/about/us.html">About us</a> <a href="https://www.php.cn/about/disclaimer.html">Disclaimer</a> <a href="https://www.php.cn/update/article_0_1.html">Sitemap</a> </div> <div class="footerbottom"> <p> © php.cn All rights reserved </p> </div> </div> </footer> <input type="hidden" id="verifycode" value="/captcha.html"> <script>layui.use(['element', 'carousel'], function () {var element = layui.element;$ = layui.jquery;var carousel = layui.carousel;carousel.render({elem: '#test1', width: '100%', height: '330px', arrow: 'always'});$.getScript('/static/js/jquery.lazyload.min.js', function () {$("img").lazyload({placeholder: "/static/images/load.jpg", effect: "fadeIn", threshold: 200, skip_invisible: false});});});</script> <script src="/static/js/common_new.js"></script> <script type="text/javascript" src="/static/js/jquery.cookie.js?1745213846"></script> <script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script> <link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css?2' type='text/css' media='all' /> <script type='text/javascript' src='/static/js/viewer.min.js?1'></script> <script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script> <script type="text/javascript" src="/static/js/global.min.js?5.5.53"></script> <script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function () { var u = "https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u + 'matomo.php']); _paq.push(['setSiteId', '9']); var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0]; g.async = true; g.src = u + 'matomo.js'; s.parentNode.insertBefore(g, s); })(); </script> <script> // top layui.use(function () { var util = layui.util; util.fixbar({ on: { mouseenter: function (type) { layer.tips(type, this, { tips: 4, fixed: true, }); }, mouseleave: function (type) { layer.closeAll("tips"); }, }, }); }); document.addEventListener("DOMContentLoaded", (event) => { // 定义一个函数来处理滚动链接的点击事件 function setupScrollLink(scrollLinkId, targetElementId) { const scrollLink = document.getElementById(scrollLinkId); const targetElement = document.getElementById(targetElementId); if (scrollLink && targetElement) { scrollLink.addEventListener("click", (e) => { e.preventDefault(); // 阻止默认链接行为 targetElement.scrollIntoView({ behavior: "smooth" }); // 平滑滚动到目标元素 }); } else { console.warn( `Either scroll link with ID '${scrollLinkId}' or target element with ID '${targetElementId}' not found.` ); } } // 使用该函数设置多个滚动链接 setupScrollLink("Article_Details_main1L2s_1", "article_main_title1"); setupScrollLink("Article_Details_main1L2s_2", "article_main_title2"); setupScrollLink("Article_Details_main1L2s_3", "article_main_title3"); setupScrollLink("Article_Details_main1L2s_4", "article_main_title4"); setupScrollLink("Article_Details_main1L2s_5", "article_main_title5"); setupScrollLink("Article_Details_main1L2s_6", "article_main_title6"); // 可以继续添加更多的滚动链接设置 }); window.addEventListener("scroll", function () { var fixedElement = document.getElementById("Article_Details_main1Lmain"); var scrollTop = window.scrollY || document.documentElement.scrollTop; // 兼容不同浏览器 var clientHeight = window.innerHeight || document.documentElement.clientHeight; // 视口高度 var scrollHeight = document.documentElement.scrollHeight; // 页面总高度 // 计算距离底部的距离 var distanceToBottom = scrollHeight - scrollTop - clientHeight; // 当距离底部小于或等于300px时,取消固定定位 if (distanceToBottom <= 980) { fixedElement.classList.remove("Article_Details_main1Lmain"); fixedElement.classList.add("Article_Details_main1Lmain_relative"); } else { // 否则,保持固定定位 fixedElement.classList.remove("Article_Details_main1Lmain_relative"); fixedElement.classList.add("Article_Details_main1Lmain"); } }); </script> </body> </html>