国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 PHP沉思錄

PHP沉思錄

Jun 08, 2016 pm 05:31 PM
java servlet

<script>ec(2);</script>



本文是一個系列的第一篇,目前想到的其他一些主題是:
  
   SQL注入問題
   事件模型
   AOP模型
   UI Framework的實現(xiàn)
   Template機制
  
  
   PHP沉思錄
   工作模型
   PHP的工作模型非常特殊。從某種程度上說,PHP和ASP、ASP.NET、JSP/Servlet等流行的Web技術,有著本質上的區(qū)別。

   以Java為例,Java在Web應用領域,有兩種技術:Java Servlet和JSP(Java Server Page)。Java Servlet是一種特殊類型的Java程序,它通過實現(xiàn)相關接口,處理Web服務器發(fā)送過來的請求,完成相應的工作。JSP在形式上是一種類似于PHP 的腳本,但是事實上,它最后也被編譯成Servlet。

也就是說,在Java解決方案中,JSP和Servlet是作為獨立的Java應用程序執(zhí)行的,它們在初始化之后就駐留內存,通過特定的接口和 Web服務器通信,完成相應工作。除非被顯式地重啟,否則它們不會終止。因此,可以在JSP和Servlet中使用各種緩存技術,例如數(shù)據(jù)庫連接池。

   ASP.NET的機制與此類似。至于ASP,雖然也是一種解釋型語言,但是仍然提供了Application對象來存放應用程序級的全局變量,它依托于ASP解釋器在IIS中駐留的進程,在整個應用程序的生命期有效。

   PHP卻完全不是這樣。作為一種純解釋型語言,PHP腳本在每次被解釋時進行初始化,在解釋完畢后終止運行。這種運行是互相獨立的,每一次請求都會創(chuàng)建一個單獨的進程或線程,來解釋相應的頁面文件。頁面創(chuàng)建的變量和其他對象,都只在當前的頁面內部可見,無法跨越頁面訪問舊電腦回收。

在終止運行后,頁面中申請的、沒有被代碼顯式釋放的外部資源,包括內存、數(shù)據(jù)庫連接、文件句柄、Socket連接等,都會被強行釋放。
   也就是說,PHP無法在語言級別直接訪問跨越頁面的變量,也無法創(chuàng)建駐留內存的對象。見下例:
  
      class StaticVarTester {
   public static $StaticVar = 0;
   }
  
   function TestStaticVar() {
   StaticVarTester :: $StaticVar += 1;
   echo "StaticVarTester :: StaticVar = " . StaticVarTester :: $StaticVar;
   }
  
   TestStaticVar();
   echo "
";
   TestStaticVar();
   ?>
  
  在這個例子中,定義了一個名為StaticVarTester的類,它僅有一個公共的靜態(tài)成員$StaticVar,并被初始化為0。然后,在 TestStaticVar()函數(shù)中,對StaticVarTester :: $StaticVar進行累加操作,并將它打印輸出。
  熟悉Java或C++的開發(fā)者對這個例子應該并不陌生。$StaticVar作為StaticVarTester類的一個靜態(tài)成員,只在類被裝載時進行初始化,無論StaticVarTester類被實例化多少次,$StaticVar都只存在一個實例,而且不會被多次初始化。因此,當?shù)谝淮握{用 TestStaticVar()函數(shù)時,$StaticVar進行了累加操作,值為1,并被保存。第二次調用TestStaticVar()函數(shù),$ StaticVar的值為2。
   打印出來的結果和我們預料的一樣:
  
   StaticVarTester :: StaticVar = 1
   StaticVarTester :: StaticVar = 2
  
   但是,當瀏覽器刷新頁面,再次執(zhí)行這段代碼時,不同的情況出現(xiàn)了。在Java或C++里面,$StaticVar的值會被保存并一直累加下去,我們將會看到如下的結果:
  
   StaticVarTester :: StaticVar = 3
   StaticVarTester :: StaticVar = 4
   …
  
   但是在PHP中,由于上文敘及的機制,當前頁面每次都解釋時,都會執(zhí)行一次程序初始化和終止的過程。也就是說,每次訪問時,StaticVarTester都會被重新裝載,而下列這行語句
  
   public static $StaticVar = 0;
  
  也會被重復執(zhí)行。當頁面執(zhí)行完成后,所有的內存空間都會被回收,$StaticVar這個變量(連同整個StaticVarTester類)也就不復存在。
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

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)

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Improving Readability with Comments Improving Readability with Comments Jul 18, 2025 am 04:46 AM

The key to writing good comments is to explain "why" rather than just "what was done" to improve the readability of the code. 1. Comments should explain logical reasons, such as considerations behind value selection or processing; 2. Use paragraph annotations for complex logic to summarize the overall idea of functions or algorithms; 3. Regularly maintain comments to ensure consistency with the code, avoid misleading, and delete outdated content if necessary; 4. Synchronously check comments when reviewing the code, and record public logic through documents to reduce the burden of code comments.

Writing Effective PHP Comments Writing Effective PHP Comments Jul 18, 2025 am 04:44 AM

Comments cannot be careless because they want to explain the reasons for the existence of the code rather than the functions, such as compatibility with old interfaces or third-party restrictions, otherwise people who read the code can only rely on guessing. The areas that must be commented include complex conditional judgments, special error handling logic, and temporary bypass restrictions. A more practical way to write comments is to select single-line comments or block comments based on the scene. Use document block comments to explain parameters and return values at the beginning of functions, classes, and files, and keep comments updated. For complex logic, you can add a line to the previous one to summarize the overall intention. At the same time, do not use comments to seal code, but use version control tools.

PHP Development Environment Setup PHP Development Environment Setup Jul 18, 2025 am 04:55 AM

The first step is to select the integrated environment package XAMPP or MAMP to build a local server; the second step is to select the appropriate PHP version according to the project needs and configure multiple version switching; the third step is to select VSCode or PhpStorm as the editor and debug with Xdebug; in addition, you need to install Composer, PHP_CodeSniffer, PHPUnit and other tools to assist in development.

Effective PHP Commenting Effective PHP Commenting Jul 18, 2025 am 04:33 AM

The key to writing PHP comments is clear, useful and concise. 1. Comments should explain the intention behind the code rather than just describing the code itself, such as explaining the logical purpose of complex conditional judgments; 2. Add comments to key scenarios such as magic values, old code compatibility, API interfaces, etc. to improve readability; 3. Avoid duplicate code content, keep it concise and specific, and use standard formats such as PHPDoc; 4. Comments should be updated synchronously with the code to ensure accuracy. Good comments should be thought from the perspective of others, reduce the cost of understanding, and become a code understanding navigation device.

PHP Commenting Syntax PHP Commenting Syntax Jul 18, 2025 am 04:56 AM

There are three common ways to use PHP comments: single-line comments are suitable for briefly explaining code logic, such as // or # for the explanation of the current line; multi-line comments /*...*/ are suitable for detailed description of the functions or classes; document comments DocBlock start with /** to provide prompt information for the IDE. When using it, you should avoid nonsense, keep updating synchronously, and do not use comments to block codes for a long time.

PHP Comparison Operators PHP Comparison Operators Jul 18, 2025 am 04:57 AM

PHP comparison operators need to pay attention to type conversion issues. 1. Use == to compare values only, and type conversion will be performed, such as 1=="1" is true; 2. Use === to require the same value as the type, such as 1==="1" is false; 3. Size comparison can be used on values and strings, such as "apple"

Understanding PHP Comments Understanding PHP Comments Jul 18, 2025 am 04:24 AM

PHP comments are parts of the code that are used to interpret logic, tag tasks, or temporarily block code and are not executed by the server. Its core functions include: 1. Improve the readability of the code, which facilitates quick understanding of others and future self; 2. Supports two formats: single-line comments (// or #) and multi-line comments (//); 3. Common uses cover function descriptions, complex logic explanations, TODO markings and disable code during debugging; 4. Effective comments should avoid duplicate code, explain the reasons rather than operations, keep it concise and add version records where necessary, thereby significantly improving code maintenance efficiency.

See all articles