關(guān)于PHP事件驅(qū)動問題之我見
Jun 21, 2016 am 09:15 AM問題
看到有人在討論PHP的事件驅(qū)動問題,本應(yīng)回復(fù)一帖。但認(rèn)為回復(fù)不足以引起大家的重視,故專開一帖詳述本人對這個問題的理解,并對一佳作進(jìn)行解釋與分析。
事件驅(qū)動這個概念是廣義的??梢栽诳蛻舳?,也可以在服務(wù)器端。
在WEB應(yīng)用上,在客戶端的事件是基于JS或是插件或是JAVAAPPLET之類的東西,基本上如果是插件或是JAVAAPPLET的話,就不屬于HTML的范疇了,而真正必須用到JS的場合其實并不多,最多就是FORM的提交或是鏈接點擊之類的基本操作,因此談?wù)撌录o太大意義。
事件驅(qū)動真正的意義并不在于可視化編程,而在于它的概念,就象OO一樣。事件驅(qū)動其實是OO的一個延伸,它的最初原型是消息機(jī)制。但是事件驅(qū)動把消息封裝成了一個可調(diào)用的函數(shù),有些類似于API中的回調(diào)函數(shù),你自己可以定義這些函數(shù)執(zhí)行的內(nèi)容。而可視化編程則把這些函數(shù)獨立出來,定義好參數(shù)(多數(shù)是現(xiàn)成的對象),讓你自己寫代碼并運用這些參數(shù)(其實是用這些對象)做一些事情。
所以,PHP有事件驅(qū)動是完全可能的,主要在于框架的設(shè)計。而要做成VB之類所謂的可視化事件驅(qū)動,則必須要有配套的集成開發(fā)環(huán)境,包括頁面設(shè)計,事件編碼,編譯轉(zhuǎn)碼之類的一系列功能才行。其實象點NET這樣的事件驅(qū)動,只不過是把一些常用的WEB元素或控件,如按鈕、文本框之類的東西封裝了一下,讓你有個可視化的界面可以設(shè)計一下,當(dāng)它編譯之后,仍然是之類的文本,只是把你的事件代碼轉(zhuǎn)為了JS或是服務(wù)器端代碼而已。而PHP主要是由于IDE不夠豐富,而且也沒有預(yù)編譯機(jī)制,所以最后提交的代碼還是最終的PHP代碼,而不是點NET的資源代碼與事件代碼的混合體(一般是符合XML規(guī)范的ASP文檔,包含了非標(biāo)準(zhǔn)的HTML代碼)。故此PHP還無法達(dá)到大家心目中狹義的所謂事件驅(qū)動編程,但其實是完全可以沒有問題的。
如果大家感興趣,不妨到www.php.net官方主頁去看一下一位中國哥們(Qiang?Xue)寫的一套基于事件驅(qū)動的PHP框架PRADO,這個還是獲得高票當(dāng)選的最佳,強(qiáng)烈推薦!請參考?http://www.zend.com/php5/contest?,你看了他的源代碼后就會理解PHP的事件驅(qū)動是怎么回事。但我認(rèn)為,在這上面,由于PHP無預(yù)編譯機(jī)制,而且過度依賴OO(雖然是用PHP5寫的代碼),造成這個框架有些龐大,且使用比較復(fù)雜,可擴(kuò)展性也不是很好。不過,其中的理念非常之好,有些想法還解決了困惑我多日的問題。我下面簡單介紹一下這個框架。
該框架用ZDE及PHP5寫成,有詳細(xì)文檔,結(jié)構(gòu)十分清晰,注釋極為充分,代碼非常易于讀懂,說明作者寫碼水平非常之高。作者明確說明,這套框架參考了ASP點NET及Borland?Delphi的概念。
這個框架在驗證性上非常之強(qiáng)(并不是指里面有什么驗證登錄之類的模塊),十分健壯,幾乎不可能有什么直接的漏洞可以從外面攻入,它是引入了規(guī)范文件這個概念做限制,很有效地解決了大量驗證時的效率瓶頸,這種驗證方法只有一個問題就是規(guī)范文件本身的制作比較費力(當(dāng)然用工具的話是另一回事了),然而一旦做好(規(guī)范文件本身有格式與規(guī)范的),驗證就自然而然地由框架去做了,而無需每次人為調(diào)用。它的事件也可以定義在規(guī)范文件之內(nèi)(我卻認(rèn)為這就沒有必要了),其實它的規(guī)范文件就有點類似于DELPHI或是VB中的FORM定義文件,只不過是用XML寫的純文本,而非可視化。而對于事件驅(qū)動,框架內(nèi)置了一套與點NET類似的基本事件流,你可以在不同階段定制這些事件,其實說白了,就是重新定義這幾個OnXXX函數(shù),用給定形式的參數(shù),你也可以自己加入自己的事件,比如你在定義自己的組件時,在規(guī)范文件中定義好該組件可能有的事件函數(shù)及參數(shù),以后你在使用該組件時可以直接定義這些被允許的函數(shù)——不過我認(rèn)為這種方式過于復(fù)雜,且要大量讀入并分析XML文件,雖然十分地嚴(yán)謹(jǐn),很安全,但有些過分了,也沒有充分利用到PHP本身的靈活性,我的思路是用類似于DELPHI的函數(shù)句柄賦值的辦法或是用C的回調(diào)函數(shù)的特性,即可在寫代碼時在任何時間任何地點定義事件,而仍然能明確事件發(fā)出者及類型并有足夠地安全性保證,且無需機(jī)械地強(qiáng)制各個組件只能有哪些事件,代碼修改及擴(kuò)展都十分方便。當(dāng)然,在做大項目的時候,嚴(yán)格的定義是必要的,不過,即使如此,該框架處理事件的方法還是有些古板。
它的模板我認(rèn)為是一個比較好的想法,它的模板有些類似于點NET的ASP文件在編譯前的文件(我對ASP點NET并不熟,但明白一些原理),但起作用的方式則類似于DELPHI的FORM文件,是一個很好的概念,唯的一缺點是用DW之類所見即所得的通用編輯器則感覺不是很順手,因為一個模板中可以同時把幾個互斥的組件放在一起,而只在運行過程中決定顯示哪些。
就我本人看該框架的代碼,還是發(fā)現(xiàn)它有一些非常弱的項。其中最主要的一個就是路徑的問題,可擴(kuò)展性很低,應(yīng)該比較適用于專用主機(jī),對一些受限主機(jī)(目錄限制或是權(quán)限限制)就無能為力了,也無相應(yīng)的提醒措施(也無相關(guān)接口)。它對某些資源或文件的路徑,用了一種繁瑣的叫assetService的機(jī)制,目的就是確定文件的路徑,作者自己也說,如果用了這個服務(wù),系統(tǒng)消耗會明顯增加,其實這個是借鑒了FLASH中asset?library的概念,它這樣雖然可以任意指定路徑,但每次都必須重新校驗,有些得不償失。我的作法則是固定好幾個主要路徑,而其的子目錄都可隨意,就綜合平衡了兩者的矛盾。由于對路徑問題缺乏考慮,導(dǎo)致該框架對語言設(shè)置、個性化模板等無能為力,如要翻譯一個項目,手續(xù)之繁,工作量之大是可想而知的,而且極易出錯。這是該框架中最嚴(yán)重的一個問題。
從總體上來說,該框架的理念上,設(shè)計上,代碼上絕對都屬一流。當(dāng)然不足總是有的,不過完全不妨礙我們研究及學(xué)習(xí)它。它的代碼我并未全看,只主要看了幾個核心程序及一些說明,但已能足夠看清楚其結(jié)構(gòu)與思想,對作者深表佩服,但對其中的不足也深表遺憾。不管怎么樣,它都絕對是研究PHP事件驅(qū)動代碼的好作品。因此強(qiáng)烈推薦!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
