{"id":59908,"date":"2024-01-12T15:35:19","date_gmt":"2024-01-12T10:05:19","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59908"},"modified":"2024-01-17T15:52:16","modified_gmt":"2024-01-17T10:22:16","slug":"php-8-unleashed-a-deep-dive-into-enhanced-features","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/php-8-unleashed-a-deep-dive-into-enhanced-features\/","title":{"rendered":"PHP 8 Unleashed: A Deep Dive into Enhanced Features"},"content":{"rendered":"<h2><strong>Introduction:<\/strong><\/h2>\n<p>PHP, a server-side scripting language powering a vast majority of websites, has taken a giant leap forward with the release of PHP 8. Packed with innovative features and improvements, PHP 8 promises to revolutionize the way developers build web applications. In this comprehensive blog post, we&#8217;ll explore the enhanced features of PHP 8, dive into real-world examples to illustrate their practical applications, and ensure that we navigate the topic with due consideration to copyright issues.<\/p>\n<h2>1: JIT Compiler &#8211; Turbocharging PHP Performance<\/h2>\n<p>Traditional PHP interpreters work by translating source code into an intermediate representation, which the Zend Engine then executes. The JIT compiler takes a different approach, translating PHP code into machine code just before execution, optimizing it for the specific system architecture.<\/p>\n<p>The primary motivation behind the JIT compiler is to improve the execution speed of PHP code significantly. By converting PHP code into machine code at runtime, repetitive tasks can be executed more efficiently, reducing overall execution time. This can profoundly impact the performance of web applications, especially those dealing with complex computations or large datasets.<br \/>\nConsider a scenario where a web application involves complex mathematical computations, such as generating statistical reports. Before PHP 8, developers might optimize the algorithm or use caching mechanisms to alleviate performance bottlenecks. With the JIT compiler, the performance of such computations can be enhanced without the need for extensive manual optimizations.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Before PHP 8<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">calculateFactorial<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$n<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-variable\">$n<\/span> &lt;= <span class=\"hljs-number\">1<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 return<\/span> <span class=\"hljs-number\">1<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 return<\/span> <span class=\"hljs-variable\">$n<\/span> * <span class=\"hljs-title function_ invoke__\">calculateFactorial<\/span>(<span class=\"hljs-variable\">$n<\/span> - <span class=\"hljs-number\">1<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ With PHP 8 JIT Compiler<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$result<\/span> = <span class=\"hljs-title function_ invoke__\">calculateFactorial<\/span>(<span class=\"hljs-number\">10<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-variable\">$result<\/span>;\r\n<\/code><\/pre>\n<\/div>\n<p>In this example, the JIT compiler optimizes the recursive factorial calculation, resulting in improved performance.<\/p>\n<h2>2: Named Arguments &#8211; Clarity and Flexibility in Function Calls<\/h2>\n<p>PHP 8 introduces named arguments, a feature designed to enhance code readability and provide developers greater flexibility when calling functions. Named arguments make function calls more readable by allowing developers to pass values by explicitly specifying parameter names.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Before PHP 8<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">createPerson<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$name<\/span>, <span class=\"hljs-variable\">$age<\/span>, <span class=\"hljs-variable\">$city<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Function logic<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-title function_ invoke__\">createPerson<\/span>(<span class=\"hljs-string\">\"John Doe\"<\/span>, <span class=\"hljs-number\">25<\/span>, <span class=\"hljs-string\">\"New York\"<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ With Named Arguments<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">createPersonWithNamedArgs<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$name<\/span>, <span class=\"hljs-variable\">$age<\/span>, <span class=\"hljs-variable\">$city<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Function logic<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-title function_ invoke__\">createPersonWithNamedArgs<\/span>(<span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"John Doe\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">25<\/span>, <span class=\"hljs-attr\">city<\/span>: <span class=\"hljs-string\">\"New York\"<\/span>);\r\n<\/code><\/pre>\n<\/div>\n<p>Named arguments improve code readability and reduce the chance of passing arguments in the wrong order.<\/p>\n<h3>Flexibility in Function Calls<\/h3>\n<p>Named arguments allow developers to skip optional parameters or specify only the ones they need. This is particularly useful when dealing with functions that have a large number of parameters.<\/p>\n<h4>Example:<\/h4>\n<pre><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Without Named Arguments<\/span>\r\n<span class=\"hljs-title function_ invoke__\">configureDatabase<\/span>(<span class=\"hljs-string\">\"localhost\"<\/span>, <span class=\"hljs-string\">\"username\"<\/span>, <span class=\"hljs-string\">\"password\"<\/span>, <span class=\"hljs-string\">\"my_database\"<\/span>, <span class=\"hljs-literal\">true<\/span>, <span class=\"hljs-literal\">true<\/span>);\r\n\r\n<span class=\"hljs-comment\">\/\/ With Named Arguments<\/span>\r\n<span class=\"hljs-title function_ invoke__\">configureDatabase<\/span>(\r\n    <span class=\"hljs-attr\">host<\/span>: <span class=\"hljs-string\">\"localhost\"<\/span>,\r\n    <span class=\"hljs-attr\">username<\/span>: <span class=\"hljs-string\">\"username\"<\/span>,\r\n    <span class=\"hljs-attr\">password<\/span>: <span class=\"hljs-string\">\"password\"<\/span>,\r\n    <span class=\"hljs-attr\">database<\/span>: <span class=\"hljs-string\">\"my_database\"<\/span>\r\n);<\/code><\/pre>\n<p>We&#8217;ve skipped the optional parameters, providing only the values for the necessary ones. This enhances the expressiveness of the code and reduces the chance of errors due to misplaced arguments.<\/p>\n<h2>3: Attributes &#8211; Adding Metadata to Code<\/h2>\n<p>Attributes, also known as annotations in other languages, allow developers to add metadata to code elements, providing additional information to frameworks and tools.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Define an attribute<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-meta\">#[Author<\/span>(<span class=\"hljs-string\">\"Jane Smith\"<\/span>)<span class=\"hljs-meta\">]<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Article<\/span><\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-meta\">\u00a0 #[Title<\/span>(<span class=\"hljs-string\">\"PHP 8 Enhanced Features\"<\/span>)<span class=\"hljs-meta\">]<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">publish<\/span>() <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \u00a0 \/\/ Publishing logic<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Accessing attributes<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$classAttributes<\/span> = <span class=\"hljs-title function_ invoke__\">attributes<\/span>(<span class=\"hljs-title class_\">Article<\/span>::<span class=\"hljs-variable language_\">class<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$methodAttributes<\/span> = <span class=\"hljs-title function_ invoke__\">attributes<\/span>([<span class=\"hljs-title class_\">Article<\/span>::<span class=\"hljs-variable language_\">class<\/span>, <span class=\"hljs-string\">'publish'<\/span>]);\r\n<\/code><\/pre>\n<\/div>\n<p>Attributes enhance code expressiveness and facilitate better documentation.<\/p>\n<h2>4: Union Types &#8211; Enhanced Type Safety<\/h2>\n<p>PHP 8 introduces union types, allowing developers to specify that a variable can have one of several types. Traditional type declarations in PHP often involve specifying a single type for a variable, limiting the flexibility to accept multiple valid types.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Before PHP 8<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">processUserData<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$data<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Process user data<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ With Union Types<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">processUserDataWithUnionType<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>|<span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-variable\">$data<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Process user data<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>Union types provide clearer type declarations, reducing the likelihood of unexpected runtime errors.<\/p>\n<h3>Enhanced Type Safety:<\/h3>\n<p>Union types contribute to enhanced type safety by explicitly declaring the acceptable types for a variable. This reduces the likelihood of runtime errors caused by unexpected types.<\/p>\n<h4>Example:<\/h4>\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">divideNumbers<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">float<\/span>|<span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-variable\">$a<\/span>, <span class=\"hljs-keyword\">float<\/span>|<span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-variable\">$b<\/span><\/span>): <span class=\"hljs-title\">float<\/span>|<span class=\"hljs-title\">int<\/span> <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 if<\/span> (<span class=\"hljs-variable\">$b<\/span> === <span class=\"hljs-number\">0<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 return<\/span> <span class=\"hljs-number\">0<\/span>; <span class=\"hljs-comment\">\/\/ Handle division by zero<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 return<\/span> <span class=\"hljs-variable\">$a<\/span> \/ <span class=\"hljs-variable\">$b<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<p>In this example, the <code>divideNumbers<\/code> function can handle both float and int types for its parameters, improving type safety.<\/p>\n<p>Union types play well with nullable types, allowing developers to specify that a variable can be of a certain type or <code>null<\/code>.<\/p>\n<h2>5: Match Expression &#8211; Simplifying Switch Statements<\/h2>\n<p>The match expression in PHP 8 is an enhanced version of the switch statement, offering a more concise syntax and improved functionality.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Before PHP 8<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getStatusMessage<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$status<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 switch<\/span> (<span class=\"hljs-variable\">$status<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 case<\/span> <span class=\"hljs-string\">'success'<\/span>:<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 \u00a0 return<\/span> <span class=\"hljs-string\">'Operation successful'<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 case<\/span> <span class=\"hljs-string\">'error'<\/span>:<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 \u00a0 return<\/span> <span class=\"hljs-string\">'An error occurred'<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 default<\/span>:<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 \u00a0 return<\/span> <span class=\"hljs-string\">'Unknown status'<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ With Match Expression<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getStatusMessageWithMatch<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$status<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 return<\/span> <span class=\"hljs-keyword\">match<\/span> (<span class=\"hljs-variable\">$status<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-string\">\u00a0 \u00a0 'success'<\/span> =&gt; <span class=\"hljs-string\">'Operation successful'<\/span>,<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-string\">\u00a0 \u00a0 'error'<\/span> =&gt; <span class=\"hljs-string\">'An error occurred'<\/span>,<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 default<\/span> =&gt; <span class=\"hljs-string\">'Unknown status'<\/span>,<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  };<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>The match expression simplifies the syntax and enhances code readability.<\/p>\n<h3>Handling Complex Conditions:<\/h3>\n<p>One of the match expression&#8217;s strengths lies in its ability to handle complex conditions more efficiently than switch statements.<\/p>\n<h4>Example:<\/h4>\n<pre><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$grade<\/span> = <span class=\"hljs-string\">'B'<\/span>;\r\n<span class=\"hljs-variable\">$result<\/span> = <span class=\"hljs-keyword\">match<\/span> (<span class=\"hljs-literal\">true<\/span>) {\r\n    <span class=\"hljs-variable\">$grade<\/span> == <span class=\"hljs-string\">'A'<\/span> =&gt; <span class=\"hljs-string\">'Excellent'<\/span>,\r\n    <span class=\"hljs-variable\">$grade<\/span> == <span class=\"hljs-string\">'B'<\/span> =&gt; <span class=\"hljs-string\">'Good'<\/span>,\r\n    <span class=\"hljs-variable\">$grade<\/span> == <span class=\"hljs-string\">'C'<\/span> =&gt; <span class=\"hljs-string\">'Average'<\/span>,\r\n    <span class=\"hljs-keyword\">default<\/span> =&gt; <span class=\"hljs-string\">'Fail'<\/span>,\r\n};\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-variable\">$result<\/span>;<\/code><\/pre>\n<p>Here, the match expression evaluates a set of conditions and returns the corresponding result based on the first true condition.<\/p>\n<h2>6: Nullsafe Operator &#8211; Mitigating Null Pointer Woes<\/h2>\n<p>Handling null values and preventing null pointer exceptions has long been a concern for developers. Traditional null checks can lead to verbose and error-prone code, especially when dealing with nested objects or arrays. The nullsafe operator (<code>?-&gt;<\/code>) simplifies and streamlines null checks, allowing developers to access properties or call methods on potentially null objects without the need for explicit checks.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Before PHP 8<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$length<\/span> = <span class=\"hljs-number\">0<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-variable\">$user<\/span> !== <span class=\"hljs-literal\">null<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">\u00a0 $address<\/span> = <span class=\"hljs-variable\">$user<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">getAddress<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 if<\/span> (<span class=\"hljs-variable\">$address<\/span> !== <span class=\"hljs-literal\">null<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">\u00a0 \u00a0 $length<\/span> = <span class=\"hljs-variable\">$address<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">getLength<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ With Nullsafe Operator<\/span>\r\n<span class=\"hljs-variable\">$length<\/span> = <span class=\"hljs-variable\">$user<\/span>?-&gt;<span class=\"hljs-title function_ invoke__\">getAddress<\/span>()?-&gt;<span class=\"hljs-title function_ invoke__\">getLength<\/span>() ?? <span class=\"hljs-number\">0<\/span>;\r\n<\/code><\/pre>\n<\/div>\n<p>In this example, if <code>$user<\/code>, <code>$user-&gt;getAddress()<\/code>, or <code>$user-&gt;getAddress()-&gt;getLength()<\/code> is null, the entire expression evaluates to 0. This helps prevent null pointer exceptions and ensures a default value in case of null. The nullsafe operator reduces the need for verbose null checks, resulting in cleaner code.<\/p>\n<p>The nullsafe operator in PHP 8 offers a powerful and concise solution to the longstanding challenge of handling null values. By simplifying the syntax for null checks, it improves code readability and reduces the risk of null pointer exceptions.<\/p>\n<h2>7: Constructor Property Promotion &#8211; Streamlining Object Initialization<\/h2>\n<p>Constructor property promotion in PHP 8 allows developers to declare and initialize class properties directly in the constructor, reducing the need for repetitive boilerplate code.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ Before PHP 8<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> Main<span class=\"hljs-title\">Points<\/span> <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-variable\">$x<\/span>;\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-variable\">$y<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-variable\">$x<\/span>, <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-variable\">$y<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable language_\">\u00a0 \u00a0 $this<\/span>-&gt;x = <span class=\"hljs-variable\">$x<\/span>;\r\n<span class=\"hljs-variable language_\">$this<\/span>-&gt;y = <span class=\"hljs-variable\">$y<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\/\/ With Constructor Property Promotion<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> Main<span class=\"hljs-title\">PointsWithPromotion<\/span> <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-variable\">$x<\/span>, <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">float<\/span> <span class=\"hljs-variable\">$y<\/span><\/span>) <\/span>{}<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>Constructor property promotion enhances code maintainability by reducing redundancy in property initialization. Constructor property promotion supports visibility modifiers (public, protected, private) and other property modifiers such as <code>static<\/code>.<\/p>\n<h3>Type Declarations with Promotion:<\/h3>\n<p>Constructor property promotion supports type declarations, enhancing code clarity and contributing to type safety.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Order<\/span> <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span>(<span class=\"hljs-params\">\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-variable\">$orderId<\/span>,\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">array<\/span> <span class=\"hljs-variable\">$items<\/span>,\r\n<span class=\"hljs-keyword\">public<\/span> DateTime <span class=\"hljs-variable\">$orderDate<\/span>\r\n<\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \u00a0 \/\/ Initialization logic<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>In this example, the <code>Order<\/code> class ensures that <code>$orderDate<\/code> is an instance of <code>DateTime<\/code>, contributing to more precise type declarations.<\/p>\n<h2>8: Error Exceptions in PHP 8<\/h2>\n<h3>Named Errors:<\/h3>\n<p>In PHP 8, named errors provide a more descriptive and meaningful way to handle errors. Previously, developers had to rely on error codes or constants to identify specific errors. Named errors make error handling more expressive and easier to understand.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">try<\/span> {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Some code that may throw an error<\/span><\/code><code class=\"!whitespace-pre hljs language-php\">\r\n} <span class=\"hljs-keyword\">catch<\/span> (MyCustomError <span class=\"hljs-variable\">$error<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\u00a0 \/\/ Handle MyCustomError<\/span>\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Caught MyCustomError: \"<\/span> . <span class=\"hljs-variable\">$error<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">getMessage<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">} <span class=\"hljs-keyword\">catch<\/span> (AnotherError <span class=\"hljs-variable\">$error<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\u00a0 \/\/ Handle AnotherError<\/span>\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Caught AnotherError: \"<\/span> . <span class=\"hljs-variable\">$error<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">getMessage<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-built_in\">Exception<\/span> <span class=\"hljs-variable\">$e<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\u00a0 \/\/ Generic catch for other exceptions<\/span>\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Caught generic exception: \"<\/span> . <span class=\"hljs-variable\">$e<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">getMessage<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>Here, <code>MyCustomError<\/code> and <code>AnotherError<\/code> are named errors that provide more context about the nature of the error being caught.<\/p>\n<h3>Throwable Interface:<\/h3>\n<p>PHP 8 introduces the <code>Throwable<\/code> interface, which is the base interface for all errors and exceptions in PHP. This unification allows developers to catch both errors and exceptions using a single catch block.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">try<\/span> {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Some code that may throw an error or exception<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">} <span class=\"hljs-keyword\">catch<\/span> (<span class=\"hljs-built_in\">Throwable<\/span> <span class=\"hljs-variable\">$throwable<\/span>) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">\u00a0 \/\/ Handle both errors and exceptions<\/span>\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Caught throwable: \"<\/span> . <span class=\"hljs-variable\">$throwable<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">getMessage<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>Now, a single catch block can handle any object that implements the <code>Throwable<\/code> interface, providing a more consistent approach to error handling.<\/p>\n<h3>Throw Expression:<\/h3>\n<p>PHP 8 introduces the throw expression, allowing exceptions to be thrown in expressions. This feature enhances the conciseness of code by enabling developers to throw exceptions directly within an expression.<\/p>\n<h4>Example:<\/h4>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">divide<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$numerator<\/span>, <span class=\"hljs-variable\">$denominator<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \/\/ Using throw expression to handle division by zero<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">\u00a0 $result<\/span> = <span class=\"hljs-variable\">$denominator<\/span> !== <span class=\"hljs-number\">0<\/span> ? <span class=\"hljs-variable\">$numerator<\/span> \/ <span class=\"hljs-variable\">$denominator<\/span> : <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Exception<\/span>(<span class=\"hljs-string\">\"Cannot divide by zero\"<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 return<\/span> <span class=\"hljs-variable\">$result<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<p>In this example, the <code>throw new Exception(...)<\/code> expression is part of the conditional operator (<code>? :<\/code>), allowing for concise error handling within the expression itself.<\/p>\n<h2>9: Weak Maps in PHP 8 &#8211; Managing Object References<\/h2>\n<p>In PHP, traditional arrays or objects that reference other objects can inadvertently prevent those objects from being garbage collected. This can lead to memory leaks and increased resource usage. Weak Maps addresses this challenge by allowing developers to associate data with objects without creating strong references. This means the object referenced in a Weak Map can still be garbage collected if no other references exist.<\/p>\n<p>The basic syntax of Weak Maps involves creating a new WeakMap object and using its <code>set<\/code> and <code>get<\/code> methods to associate and retrieve data, respectively.<\/p>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<h4>Example:<\/h4>\n<\/div>\n<\/div>\n<pre><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$weakMap<\/span> = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">WeakMap<\/span>();\r\n\r\n<span class=\"hljs-comment\">\/\/ Associating data with an object<\/span>\r\n<span class=\"hljs-variable\">$object<\/span> = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">stdClass<\/span>();\r\n<span class=\"hljs-variable\">$weakMap<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">set<\/span>(<span class=\"hljs-variable\">$object<\/span>, <span class=\"hljs-string\">'Associated Data'<\/span>);\r\n\r\n<span class=\"hljs-comment\">\/\/ Retrieving data from the Weak Map<\/span>\r\n<span class=\"hljs-variable\">$data<\/span> = <span class=\"hljs-variable\">$weakMap<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">get<\/span>(<span class=\"hljs-variable\">$object<\/span>);\r\n\r\n<span class=\"hljs-comment\">\/\/ Removing the association<\/span>\r\n<span class=\"hljs-variable\">$weakMap<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">delete<\/span>(<span class=\"hljs-variable\">$object<\/span>);<\/code><\/pre>\n<h3>Caching:<\/h3>\n<p>Weak Maps are useful for implementing caching mechanisms where data is associated with objects but doesn&#8217;t prevent those objects from being garbage collected.<\/p>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<h4>Example:<\/h4>\n<\/div>\n<\/div>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Cache<\/span> <\/span>{<\/code><\/pre>\n<p><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 private<\/span> <span class=\"hljs-built_in\">WeakMap<\/span> <span class=\"hljs-variable\">$cache<\/span>;<\/code><\/p>\n<p><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span>() <\/span>{<\/p>\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable language_\">\u00a0 \u00a0 $this<\/span>-&gt;cache = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">WeakMap<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getData<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">object<\/span> <span class=\"hljs-variable\">$key<\/span><\/span>): ?<span class=\"hljs-title\">string<\/span><\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 return<\/span> <span class=\"hljs-variable language_\">$this<\/span>-&gt;cache-&gt;<span class=\"hljs-title function_ invoke__\">get<\/span>(<span class=\"hljs-variable\">$key<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">cacheData<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">object<\/span> <span class=\"hljs-variable\">$key<\/span>, <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-variable\">$data<\/span><\/span>) <\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable language_\">\u00a0 \u00a0 $this<\/span>-&gt;cache-&gt;<span class=\"hljs-title function_ invoke__\">set<\/span>(<span class=\"hljs-variable\">$key<\/span>, <span class=\"hljs-variable\">$data<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<h3>Memoisation:<\/h3>\n<p>Weak Maps can be used in memoisation to store calculated values associated with specific objects, allowing the objects to be garbage collected when no longer needed.<\/p>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">\n<h4>Example:<\/h4>\n<\/div>\n<\/div>\n<div class=\"bg-black rounded-md\">\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Calculator<\/span> <\/span>{<\/code><\/pre>\n<p><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 private<\/span> <span class=\"hljs-built_in\">WeakMap<\/span> <span class=\"hljs-variable\">$results<\/span>;<\/code><\/p>\n<p><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span>() <\/span>{<\/p>\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable language_\">\u00a0 \u00a0 $this<\/span>-&gt;results = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">WeakMap<\/span>();<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">calculateExpensiveOperation<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">object<\/span> <span class=\"hljs-variable\">$input<\/span><\/span>): <span class=\"hljs-title\">int<\/span><\/span>{<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \u00a0 \/\/ Check if result is already memoized<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 if<\/span> (<span class=\"hljs-variable language_\">$this<\/span>-&gt;results-&gt;<span class=\"hljs-title function_ invoke__\">has<\/span>(<span class=\"hljs-variable\">$input<\/span>)) {<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">\u00a0 \u00a0 \u00a0 return<\/span> <span class=\"hljs-variable language_\">$this<\/span>-&gt;results-&gt;<span class=\"hljs-title function_ invoke__\">get<\/span>(<span class=\"hljs-variable\">$input<\/span>);<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">  \u00a0 }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \u00a0 \/\/ Perform the expensive operation<\/span><\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">\u00a0 \u00a0 $result<\/span> = <span class=\"hljs-comment\">\/* perform calculation *\/<\/span>;<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-comment\">  \u00a0 \/\/ Memoize the result<\/span><\/code><\/pre>\n<p><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable language_\">\u00a0 \u00a0 $this<\/span>-&gt;results-&gt;<span class=\"hljs-title function_ invoke__\">set<\/span>(<span class=\"hljs-variable\">$input<\/span>, <span class=\"hljs-variable\">$result<\/span>);<\/code><\/p>\n<p><span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable\">$result<\/span>;<\/p>\n<pre class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\">  }<\/code>\r\n<code class=\"!whitespace-pre hljs language-php\">}\r\n<\/code><\/pre>\n<\/div>\n<h3>Benefits of Weak Maps:<\/h3>\n<ol>\n<li><strong>Preventing Memory Leaks:<\/strong> Weak Maps prevent unintentional memory leaks by allowing objects to be garbage collected when no longer in use.<\/li>\n<li><strong>Cleaner Resource Management:<\/strong> When dealing with object references, Weak Maps provides a cleaner way to associate data without creating strong references that impede garbage collection.<\/li>\n<li><strong>Efficient Caching:<\/strong> In scenarios where caching is involved, Weak Maps ensures that cached data is associated with objects only as long as those objects are reachable.<\/li>\n<\/ol>\n<h2>Conclusion: Embracing the Future of PHP Development<\/h2>\n<p>In conclusion, PHP 8 has ushered in a new era for PHP development, offering a suite of features that enhance performance, readability, and maintainability. As developers, embracing these enhancements can lead to more efficient and robust codebases. However, it&#8217;s crucial to remain mindful of copyright issues when incorporating code examples and snippets from external sources. Always attribute and seek permission when necessary, ensuring ethical and responsible sharing of knowledge within the developer community. With PHP 8, the future of web development looks brighter than ever, and by staying informed and respecting intellectual property rights, developers can navigate this exciting landscape responsibly.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction: PHP, a server-side scripting language powering a vast majority of websites, has taken a giant leap forward with the release of PHP 8. Packed with innovative features and improvements, PHP 8 promises to revolutionize the way developers build web applications. In this comprehensive blog post, we&#8217;ll explore the enhanced features of PHP 8, dive [&hellip;]<\/p>\n","protected":false},"author":1527,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[3602],"tags":[5261,227,477,5597],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59908"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1527"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59908"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59908\/revisions"}],"predecessor-version":[{"id":60084,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59908\/revisions\/60084"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}