{"id":508,"date":"2024-07-16T19:42:03","date_gmt":"2024-07-16T19:42:03","guid":{"rendered":"https:\/\/zalvis.com\/blog\/?p=508"},"modified":"2025-05-04T18:30:23","modified_gmt":"2025-05-04T18:30:23","slug":"10-things-not-to-do-in-php-8","status":"publish","type":"post","link":"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html","title":{"rendered":"10 Things Not to Do in PHP 8"},"content":{"rendered":"<p>PHP 8 brings a host of new features, performance improvements, and syntactic changes that make it a more powerful and efficient programming language. However, with these new features come potential pitfalls that developers should avoid to maintain code quality, performance, and security. In this article, we will explore 10 things not to do in PHP 8 to ensure that your code remains robust, maintainable, and efficient.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"10_Things_Not_to_Do_in_PHP_8\"><\/span>10 Things Not to Do in PHP 8<span class=\"ez-toc-section-end\"><\/span><\/h2><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#10_Things_Not_to_Do_in_PHP_8\" >10 Things Not to Do in PHP 8<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#1_Ignoring_Deprecated_Functions_and_Features\" >1. Ignoring Deprecated Functions and Features<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#2_Using_assert_in_Production\" >2. Using assert() in Production<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#3_Neglecting_Type_Safety\" >3. Neglecting Type Safety<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#4_Overusing_Magic_Methods\" >4. Overusing Magic Methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#5_Failing_to_Handle_Exceptions_Properly\" >5. Failing to Handle Exceptions Properly<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#6_Using_eval\" >6. Using eval()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#7_Ignoring_Error_Reporting_and_Logging\" >7. Ignoring Error Reporting and Logging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#8_Not_Using_New_PHP_8_Features\" >8. Not Using New PHP 8 Features<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#9_Using_Outdated_or_Unsupported_Libraries\" >9. Using Outdated or Unsupported Libraries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#10_Neglecting_Code_Reviews_and_Testing\" >10. Neglecting Code Reviews and Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/zalvis.com\/blog\/10-things-not-to-do-in-php-8.html\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-511\" src=\"https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1.png\" alt=\"10 Things Not to Do in PHP 8\" width=\"1000\" height=\"500\" srcset=\"https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1.png 1000w, https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1-300x150.png 300w, https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1-768x384.png 768w, https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1-720x360.png 720w, https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1-580x290.png 580w, https:\/\/zalvis.com\/blog\/wp-content\/uploads\/2024\/07\/Zalvis-Blog-1-320x160.png 320w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Ignoring_Deprecated_Functions_and_Features\"><\/span>1. Ignoring Deprecated Functions and Features<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>With each new PHP release, some functions and features are deprecated and eventually removed. PHP 8 is no exception. Ignoring these deprecations can lead to broken code when the deprecated features are removed in future releases. For example, the <em>create_function()<\/em> has been deprecated in PHP 7.2 and removed in PHP 8.0. Using such deprecated functions can lead to compatibility issues and maintenance headaches.<\/p>\n<p><strong>Solution<\/strong>: Regularly review the PHP manual for deprecated features and update your code accordingly. Use tools like PHP_CodeSniffer and PHPStan to detect deprecated code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_assert_in_Production\"><\/span>2. Using assert() in Production<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <em>assert()<\/em> function is a useful debugging tool that can be used to test assumptions in your code. However, using <em>assert()<\/em> in production code is not recommended because it can introduce security vulnerabilities if assertions are not carefully managed.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>\/\/ Dangerous use of assert<\/code><br \/>\n<code>assert($user_input === 'expected_value');<\/code><\/p>\n<p>If the assertion is not met, the <em>assert()<\/em> function can execute arbitrary code, leading to potential security risks.<\/p>\n<p><strong>Solution<\/strong>: Remove or disable assert() statements in production code. Use proper validation and error handling mechanisms instead.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Neglecting_Type_Safety\"><\/span>3. Neglecting Type Safety<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>PHP 8 introduces stricter type enforcement, making it easier to write type-safe code. However, neglecting type safety can lead to subtle bugs and unexpected behavior.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>function addNumbers($a, $b) {<\/code><br \/>\n<code>return $a + $b;<\/code><br \/>\n<code>}<\/code><\/p>\n<p><code>\/\/ Passing non-numeric values<\/code><br \/>\n<code>echo addNumbers('5', '10'); \/\/ Outputs: 510 (concatenation)<\/code><\/p>\n<p><strong>Solution<\/strong>: Use strict typing and type declarations to enforce type safety.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>declare(strict_types=1);<\/code><\/p>\n<p><code>function addNumbers(int $a, int $b): int {<\/code><br \/>\n<code>return $a + $b;<\/code><br \/>\n<code>}<\/code><br \/>\n<code>echo addNumbers(5, 10); \/\/ Outputs: 15<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Overusing_Magic_Methods\"><\/span>4. Overusing Magic Methods<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Magic methods like <em>__get(), __set(), __call()<\/em>, and others can be powerful, but overusing them can lead to code that is difficult to understand and maintain. Magic methods can obscure the flow of the program and make debugging more challenging.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>class Example {<\/code><br \/>\n<code>public function __call($name, $arguments) {<\/code><br \/>\n<code>\/\/ Handle dynamic method calls<\/code><br \/>\n<code>}<\/code><br \/>\n<code>}<\/code><\/p>\n<p><strong>Solution<\/strong>: Use explicit methods and properties whenever possible. Reserve magic methods for truly dynamic behavior that cannot be achieved otherwise.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Failing_to_Handle_Exceptions_Properly\"><\/span>5. Failing to Handle Exceptions Properly<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>PHP 8 continues to improve its exception handling mechanisms, but failing to handle exceptions properly can lead to unhandled errors and poor user experience.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>try {<\/code><br \/>\n<code>$result = riskyFunction();<\/code><br \/>\n<code>} catch (Exception $e) {<\/code><br \/>\n<code>\/\/ Empty catch block<\/code><br \/>\n<code>}<\/code><\/p>\n<p><strong>Solution<\/strong>: Handle exceptions meaningfully by logging errors, providing user-friendly error messages, and ensuring that the application can recover or fail gracefully.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>try {<\/code><br \/>\n<code>$result = riskyFunction();<\/code><br \/>\n<code>} catch (Exception $e) {<\/code><br \/>\n<code>error_log($e-&gt;getMessage());<\/code><br \/>\n<code>echo \"An error occurred. Please try again later.\";<\/code><br \/>\n<code>}<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Using_eval\"><\/span>6. Using eval()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The <em>eval()<\/em> function is inherently dangerous as it allows execution of arbitrary PHP code. This can introduce significant security vulnerabilities, especially if the code being evaluated includes user input.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>$code = $_GET['code'];<\/code><br \/>\n<code>eval($code); \/\/ Extremely dangerous<\/code><\/p>\n<p><strong>Solution<\/strong>: Avoid using <em>eval()<\/em>. Use safer alternatives like include or require for including PHP code, and carefully validate and sanitize any dynamic code execution needs.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_Ignoring_Error_Reporting_and_Logging\"><\/span>7. Ignoring Error Reporting and Logging<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Error reporting and logging are crucial for identifying and fixing issues in your code. Ignoring these can make debugging difficult and can lead to undetected bugs.<\/p>\n<p><strong>Solution<\/strong>: Configure PHP to display and log errors during development and disable error display in production while keeping logging enabled.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>\/\/ Development environment<\/code><br \/>\n<code>ini_set('display_errors', 1);<\/code><br \/>\n<code>ini_set('display_startup_errors', 1);<\/code><br \/>\n<code>error_reporting(E_ALL);<\/code><\/p>\n<p><code>\/\/ Production environment<\/code><br \/>\n<code>ini_set('display_errors', 0);<\/code><br \/>\n<code>error_reporting(E_ALL);<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_Not_Using_New_PHP_8_Features\"><\/span>8. Not Using New PHP 8 Features<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>PHP 8 introduces several new features like the JIT compiler, union types, named arguments, attributes, and more. Not leveraging these features can result in missing out on performance improvements and cleaner code.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>\/\/ Union types<\/code><br \/>\n<code>function processInput(int|string $input) {<\/code><br \/>\n<code>if (is_int($input)) {<\/code><br \/>\n<code>\/\/ Handle integer input<\/code><br \/>\n<code>} else {<\/code><br \/>\n<code>\/\/ Handle string input<\/code><br \/>\n<code>}<\/code><br \/>\n<code>}<\/code><\/p>\n<p><code>\/\/ Named arguments<\/code><br \/>\n<code>$result = myFunction(arg1: 'value1', arg2: 'value2');<\/code><\/p>\n<p><strong>Solution<\/strong>: Stay updated with the latest PHP features and incorporate them into your codebase to improve performance and readability.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"9_Using_Outdated_or_Unsupported_Libraries\"><\/span>9. Using Outdated or Unsupported Libraries<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Using outdated or unsupported libraries can lead to security vulnerabilities, compatibility issues, and lack of access to new features and improvements.<\/p>\n<p><strong>Solution<\/strong>: Regularly update your dependencies and use tools like Composer to manage them. Check for library support and maintenance before including them in your project.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code>{<\/code><br \/>\n<code>\"require\": {<\/code><br \/>\n<code>\"monolog\/monolog\": \"^2.0\"<\/code><br \/>\n<code>}<\/code><br \/>\n<code>}<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"10_Neglecting_Code_Reviews_and_Testing\"><\/span>10. Neglecting Code Reviews and Testing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Code reviews and testing are essential practices for maintaining code quality. Neglecting these can result in bugs, security issues, and technical debt.<\/p>\n<p><strong>Solution<\/strong>: Implement code review processes and use automated testing frameworks to ensure code quality and functionality.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<p><code># Running PHPUnit tests<\/code><br \/>\n<code>phpunit --configuration phpunit.xml<\/code><\/p>\n<p><code># Example PHPUnit test<\/code><br \/>\n<code>use PHPUnit\\Framework\\TestCase;<\/code><\/p>\n<p><code>class ExampleTest extends TestCase {<\/code><br \/>\n<code>public function testAddition() {<\/code><br \/>\n<code>$this-&gt;assertEquals(4, 2 + 2);<\/code><br \/>\n<code>}<\/code><br \/>\n<code>}<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>PHP 8 brings many enhancements that can significantly improve your development workflow, performance, and code quality. However, it also introduces potential pitfalls that developers should avoid. By paying attention to deprecated features, type safety, exception handling, and new PHP 8 features, and by avoiding insecure practices like using <em>eval()<\/em>, overusing magic methods, and neglecting error reporting and testing, you can ensure that your PHP code remains robust, secure, and maintainable. Regularly updating libraries, conducting code reviews, and leveraging new PHP 8 capabilities will help you stay ahead in the ever-evolving world of PHP development.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP 8 brings a host of new features, performance improvements, and syntactic changes that make it a more powerful and efficient programming language. However, with these new features come potential pitfalls that developers should avoid to maintain code quality, performance, and security. In this article, we will explore 10 things not to do in PHP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":511,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-508","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hosting-performance"],"_links":{"self":[{"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/posts\/508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/comments?post=508"}],"version-history":[{"count":0,"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/posts\/508\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/media\/511"}],"wp:attachment":[{"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/media?parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/categories?post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zalvis.com\/blog\/wp-json\/wp\/v2\/tags?post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}