在Mac上使用Homebrew升級到PHP 8
本文翻譯自:https://stitcher.io/blog/php-8-upgrade-mac
用Homebrew升級
首先要確保Brew是最新的:
brew update
接下來,升級PHP:
brew upgrade php
透過執行php -v以下指令檢查目前版本:
php -v
重新啟動Nginx或Apache:
sudo nginx -s reload sudo apachectl restart
並透過存取下列腳本確保本機Web伺服器也使用PHP 8:
# index.php, accessible to your web server phpinfo();
版本應顯示8.0.x。
注意:如果您使用的是Laravel Valet,請繼續閱讀,您需要一些額外的步驟才能讓Web伺服器正常運作。
Valet
如果您使用的是Laravel Valet,則應執行下列步驟對其進行升級:
composer global update
現在執行valet install:
valet install
擴充程式
#PHP擴充功能是使用pecl安裝的。我個人使用Imagick,Redis和Xdebug。它們可以這樣安裝:
pecl install imagick pecl install redis pecl install xdebug
您可以執行pecl list以查看安裝了哪些擴充功能:
pecl list # Installed packages, channel pecl.php.net: # ========================================= # Package Version State # imagick 3.4.4 stable # redis 5.1.1 stable # xdebug 2.8.0 stable
您可以使用來搜尋其他擴充功能pecl search:
pecl search pdf # Retrieving data...0% # .. # Matched packages, channel pecl.php.net: # ======================================= # Package Stable/(Latest) Local # pdflib 4.1.2 (stable) Creating PDF on the fly with the PDFlib library
安裝新軟體包後,請確保重新啟動Web伺服器:
sudo nginx -s reload sudo apachectl restart
如果您使用的是Laravel Valet,則也應重新啟動它。
valet restart
透過檢查PHP Web伺服器和CLI安裝,確保正確安裝和載入了所有擴充功能:
php -i | grep redis var_dump(extension_loaded('redis'));
如果擴充功能未正確加載,則有兩個簡單的修復程式。
首先,請確保將副檔名新增至正確的ini檔案。您可以執行php --ini以了解哪個檔案已載入:
Configuration File (php.ini) Path: /usr/local/etc/php/7.4 Loaded Configuration File: /usr/local/etc/php/7.4/php.ini Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d Additional .ini files parsed: /usr/local/etc/php/7.4/conf.d/ext-opcache.ini, /usr/local/etc/php/7.4/conf.d/php-memory-limits.ini
現在檢查ini檔案:
extension="redis.so" extension="imagick.so" zend_extension="xdebug.so"
請注意,如果您正在透過CLI測試已安裝的擴充程序,則在更改ini設定時無需重新啟動nginx,apache或Valet。
如果要從也使用pecl安裝擴充功能的舊PHP版本進行更新,則可以做第二件事。是分別重新安裝每個擴充功能。
pecl uninstall imagick pecl install imagick
最後一步
最後,您應該測試和升級專案以實現PHP 8相容性。
更多PHP8相關特性,請造訪PHP8專題欄位!
以上是在Mac上使用Homebrew升級到PHP 8的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

NamedargumentsinPHP8allowpassingvaluestoafunctionbyspecifyingtheparameternameinsteadofrelyingonparameterorder.1.Theyimprovecodereadabilitybymakingfunctioncallsself-documenting,asseeninexampleslikeresizeImage(width:100,height:50,preserveRatio:true,ups

ThestaticreturntypeinPHP8meansthemethodisexpectedtoreturnaninstanceoftheclassit'scalledon,includinganychildclass.1.Itenableslatestaticbinding,ensuringthereturnedvaluematchesthecallingclass'stype.2.Comparedtoself,whichalwaysreferstothedefiningclass,an

JITinPHP8improvesperformancebycompilingfrequentlyexecutedcodeintomachinecodeatruntime.Insteadofinterpretingopcodeseachtime,JITidentifieshotsectionsofcode,compilesthemintonativemachinecode,cachesitforreuse,andreducesinterpretationoverhead.Ithelpsmosti

constructorPropertyPromotionInphp8allowsautomaticCreationAndAssignmentOfClassPropertiesDirectlyFromConstructorParameters.insteadofMerallyAssigningEachPropertyInsideTheConstructor,developerersCanaddanAccessmodifier(公共,受保護,Orprivate,Orprivate)totheparam

PHP8的mixed類型允許變量、參數或返回值接受任何類型。 1.mixed適用於需要高度靈活性的場景,如中間件、動態數據處理和遺留代碼集成;2.它不同於union類型,因涵蓋所有可能類型,包括未來新增類型;3.使用時應保持謹慎,避免削弱類型安全性,並建議配合phpDoc說明預期類型。合理使用mixed可在保持類型提示優勢的同時提升代碼表達能力。

PHP8的match表達式通過嚴格比較提供更簡潔的條件映射。 1.使用嚴格相等(===)避免類型轉換;2.無需break語句防止意外貫穿;3.直接返回值可賦給變量;4.支持多條件合併共享結果。適用於精確匹配、映射輸入輸出場景,如HTTP狀態碼處理;不適用於範圍檢查或需要鬆散比較的情況。

PHP8的性能提升主要来自新引入的JIT编译器和Zend引擎优化,但实际应用中的收益因场景而异。1.JIT编译器在运行时将部分代码编译为机器码,显著提升CLI脚本或长时API的性能,但在短生命周期的Web请求中作用有限;2.OPcache改进增强了操作码缓存和预加载功能,减少磁盘I/O和解析开销,尤其利于Laravel或Symfony等框架;3.多项内部优化如更高效的字符串和数组操作、更小的内存占用等,虽每次提升微小但积少成多;4.实际性能提升视应用场景而定,在计算密集型任务中PHP8可快10–

PHP8的attributes通過結構化方式為代碼元素添加元數據。 1.它們使用#[]語法附加在類、方法等上方,如#[Route('/home')]定義路由;2.與PHPDoc相比更安全,具備類型檢查和編譯時驗證;3.自定義attribute需定義類並應用,例如用ReflectionAttribute創建LogExecution日誌屬性;4.常見於框架中處理路由、驗證、ORM映射等任務,提升了代碼可讀性和分離邏輯配置;5.可通過反射訪問,但應避免過度使用以免影響代碼清晰度。
