{"id":95,"date":"2021-03-08T00:17:59","date_gmt":"2021-03-08T00:17:59","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=95"},"modified":"2025-04-07T11:37:19","modified_gmt":"2025-04-07T11:37:19","slug":"php-magic-methods","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-magic-methods\/","title":{"rendered":"PHP Magic Methods"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about PHP magic methods that override the default actions when the object performs the actions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-magic-methods'>Introduction to PHP magic methods <a href=\"#introduction-to-php-magic-methods\" class=\"anchor\" id=\"introduction-to-php-magic-methods\" title=\"Anchor for Introduction to PHP magic methods\">#<\/a><\/h2>\n\n\n\n<p>PHP magic methods are special methods in a <a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">class<\/a>. The magic methods override the default actions when the object performs the actions.<\/p>\n\n\n\n<p>By convention, the names of magic methods start with a double underscore (<code>__<\/code>). And PHP reserves the methods whose names start with a double underscore (<code>__<\/code>) for magic methods. <\/p>\n\n\n\n<p>So far, you have learned that the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-constructors\/\">constructor<\/a> and <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-destructor\/\">destructor<\/a> use the <code>__construct()<\/code> and <code>__destruct()<\/code> methods. In fact, the constructor and destructor are also magic methods.<\/p>\n\n\n\n<p>The <code>__construct()<\/code> method is invoked automatically when an object is created and the <code>__destruct()<\/code> is called when the object is deleted.<\/p>\n\n\n\n<p>Besides the <code>__contruct()<\/code> and <code>__destruct()<\/code> methods, PHP also has the following magic methods:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Magic Method<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__call\/\">__call()<\/a><\/td><td>is triggered when invoking an inaccessible instance method<\/td><\/tr><tr><td><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__callstatic\/\">__callStatic()<\/a><\/td><td>is triggered when invoking an inaccessible static method<\/td><\/tr><tr><td>__get()<\/td><td>is invoked when reading the value from a non-existing or inaccessible property<\/td><\/tr><tr><td>&nbsp;__set()<\/td><td>is invoked when writing a value to a non-existing or inaccessible property<\/td><\/tr><tr><td>&nbsp;__isset()<\/td><td>is triggered by calling&nbsp;isset()&nbsp;or&nbsp;empty()&nbsp;on a non-existing or inaccessible property<\/td><\/tr><tr><td>__unset()<\/td><td>is invoked when&nbsp;unset()&nbsp;is used on a non-existing or inaccessible property.<\/td><\/tr><tr><td>__sleep()<\/td><td>The __sleep()&nbsp;commits the pending data <\/td><\/tr><tr><td>__wakeup()<\/td><td>is invoked when the unserialize() runs to reconstruct any resource that an object may have.<\/td><\/tr><tr><td>__serialize()<\/td><td>The serialize() calls __serialize(), if available, and construct and return an associative array of key\/value pairs that represent the serialized form of the object.<\/td><\/tr><tr><td>__unserialize()<\/td><td>The unserialize() calls __unserialize(), if avaialble, and restore the properties of the object from the array returned by the __unserialize() method.<\/td><\/tr><tr><td><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__tostring\/\">__toString()<\/a><\/td><td>is invoked when an object of a class is treated as a string.<\/td><\/tr><tr><td>__invoke()<\/td><td>is invoked when an object is called as a function<\/td><\/tr><tr><td>__set_state()<\/td><td>is called for a class exported by var_export()<\/td><\/tr><tr><td><a href=\"https:\/\/phptutorial.net\/php-oop\/php-clone-object\/\">__clone()<\/a><\/td><td>is called once the cloning is complete<\/td><\/tr><tr><td>__debugInfo()<\/td><td>is called by <code>var_dump()<\/code> when dumping an object to get the properties that should be shown.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This tutorial will focus on the <code>__set()<\/code> and <code>__get()<\/code> methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-__set-method'>PHP __set() method <a href=\"#php-__set-method\" class=\"anchor\" id=\"php-__set-method\" title=\"Anchor for PHP __set() method\">#<\/a><\/h2>\n\n\n\n<p>When you attempt to write to a non-existing or inaccessible property, PHP calls the <code>__set()<\/code> method automatically. The following shows the syntax of the <code>__set()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">public<\/span> __set ( string $name , mixed $value ) : void<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>__set()<\/code> method accepts the name and value of the property that you write to. The following example illustrates how to use the <code>__set()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">HtmlElement<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $attributes = &#91;];\n\n\t<span class=\"hljs-keyword\">private<\/span> $tag;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($tag)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;tag = $tag;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__set<\/span><span class=\"hljs-params\">($name, $value)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;attributes&#91;$name] = $value;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">html<\/span><span class=\"hljs-params\">($innerHTML = <span class=\"hljs-string\">''<\/span>)<\/span>\n\t<\/span>{\n\t\t$html = <span class=\"hljs-string\">\"&lt;{$this-&gt;tag}\"<\/span>;\n\t\t<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">$this<\/span>-&gt;attributes <span class=\"hljs-keyword\">as<\/span> $key =&gt; $value) {\n\t\t\t$html .= <span class=\"hljs-string\">' '<\/span> . $key . <span class=\"hljs-string\">'=\"'<\/span> . $value . <span class=\"hljs-string\">'\"'<\/span>;\n\t\t}\n\t\t$html .= <span class=\"hljs-string\">'&gt;'<\/span>;\n\t\t$html .= $innerHTML;\n\t\t$html .= <span class=\"hljs-string\">\"&lt;\/$this-&gt;tag&gt;\"<\/span>;\n\n\t\t<span class=\"hljs-keyword\">return<\/span> $html;\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define the <code>HTMLElement<\/code> class that has only one property <code>$attributes<\/code>. It will hold all the attributes of the HTML element e.g., id and class.<\/li>\n\n\n\n<li>Second, initialize the constructor with a tag name. The tag name can be any string such as div, article, main, and section.<\/li>\n\n\n\n<li>Third, implement the <code>__set()<\/code> method that adds any property to the <code>$attribute<\/code> array.<\/li>\n\n\n\n<li>Fourth, define the <code>html()<\/code> method that returns the HTML representation of the element.<\/li>\n<\/ul>\n\n\n\n<p>The following uses the <code>HTMLElement<\/code> class and create a new div element:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'HTMLElement.php'<\/span>;\n\n$div = <span class=\"hljs-keyword\">new<\/span> HtmlElement(<span class=\"hljs-string\">'div'<\/span>);\n\n$div-&gt;id = <span class=\"hljs-string\">'page'<\/span>;\n$div-&gt;class = <span class=\"hljs-string\">'light'<\/span>;\n\n<span class=\"hljs-keyword\">echo<\/span> $div-&gt;html(<span class=\"hljs-string\">'Hello'<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEh0bWxFbGVtZW50CnsKCXByaXZhdGUgJGF0dHJpYnV0ZXMgPSBbXTsKCglwcml2YXRlICR0YWc7CgoJcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCR0YWcpCgl7CgkJJHRoaXMtPnRhZyA9ICR0YWc7Cgl9CgoJcHVibGljIGZ1bmN0aW9uIF9fc2V0KCRuYW1lLCAkdmFsdWUpCgl7CgkJJHRoaXMtPmF0dHJpYnV0ZXNbJG5hbWVdID0gJHZhbHVlOwoJfQoKCXB1YmxpYyBmdW5jdGlvbiBodG1sKCRpbm5lckhUTUwgPSAnJykKCXsKCQkkaHRtbCA9ICI8eyR0aGlzLT50YWd9IjsKCQlmb3JlYWNoICgkdGhpcy0-YXR0cmlidXRlcyBhcyAka2V5ID0-ICR2YWx1ZSkgewoJCQkkaHRtbCAuPSAnICcgLiAka2V5IC4gJz0iJyAuICR2YWx1ZSAuICciJzsKCQl9CgkJJGh0bWwgLj0gJz4nOwoJCSRodG1sIC49ICRpbm5lckhUTUw7CgkJJGh0bWwgLj0gIjwvJHRoaXMtPnRhZz4iOwoKCQlyZXR1cm4gJGh0bWw7Cgl9Cn0KCiRkaXYgPSBuZXcgSHRtbEVsZW1lbnQoJ2RpdicpOwoKJGRpdi0-aWQgPSAncGFnZSc7CiRkaXYtPmNsYXNzID0gJ2xpZ2h0JzsKCmVjaG8gJGRpdi0-aHRtbCgnSGVsbG8nKTs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"page\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"light\"<\/span>&gt;<\/span>Hello<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following code attempts to write to the non-existing property: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$div-&gt;id = <span class=\"hljs-string\">'page'<\/span>;\n$div-&gt;class = <span class=\"hljs-string\">'light'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>PHP calls the <code>__set()<\/code> method implictily and adds these properties to the <code>$attribute<\/code> property.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-__get-method'>PHP __get() method <a href=\"#php-__get-method\" class=\"anchor\" id=\"php-__get-method\" title=\"Anchor for PHP&lt;code&gt; __get()&lt;\/code&gt; method\">#<\/a><\/h2>\n\n\n\n<p>When you attempt to access a property that doesn&#8217;t exist or a property that is in-accessible e.g., private or protected property, PHP automatically calls the <code>__get()<\/code> method. <\/p>\n\n\n\n<p>The <code>__get()<\/code> method accepts one argument which is the name of the property that you want to access:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">public<\/span> __get ( string $name ) : mixed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following adds the <code>__get()<\/code> method to the <code>HTMLElement<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">HtmlElement<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $attributes = &#91;];\n\n\t<span class=\"hljs-keyword\">private<\/span> $tag;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($tag)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;tag = $tag;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__set<\/span><span class=\"hljs-params\">($name, $value)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;attributes&#91;$name] = $value;\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__get<\/span><span class=\"hljs-params\">($name)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> (array_key_exists($name, <span class=\"hljs-keyword\">$this<\/span>-&gt;attributes)) {\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;attributes&#91;$name];\n\t\t}\n\t}\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">html<\/span><span class=\"hljs-params\">($innerHTML = <span class=\"hljs-string\">''<\/span>)<\/span>\n\t<\/span>{\n\t\t$html = <span class=\"hljs-string\">\"&lt;{$this-&gt;tag}\"<\/span>;\n\t\t<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">$this<\/span>-&gt;attributes <span class=\"hljs-keyword\">as<\/span> $key =&gt; $value) {\n\t\t\t$html .= <span class=\"hljs-string\">' '<\/span> . $key . <span class=\"hljs-string\">'=\"'<\/span> . $value . <span class=\"hljs-string\">'\"'<\/span>;\n\t\t}\n\t\t$html .= <span class=\"hljs-string\">'&gt;'<\/span>;\n\t\t$html .= $innerHTML;\n\t\t$html .= <span class=\"hljs-string\">\"&lt;\/$this-&gt;tag&gt;\"<\/span>;\n\n\t\t<span class=\"hljs-keyword\">return<\/span> $html;\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>__get()<\/code> method checks if the requested property exists in the <code>$attributes<\/code> before returning the result.<\/p>\n\n\n\n<p>The following creates a new <code>article<\/code> element, sets the id and class attributes, and then shows the value of these attributes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'HTMLElement.php'<\/span>;\n\n$article = <span class=\"hljs-keyword\">new<\/span> HtmlElement(<span class=\"hljs-string\">'article'<\/span>);\n\n$article-&gt;id = <span class=\"hljs-string\">'main'<\/span>;\n$article-&gt;class = <span class=\"hljs-string\">'light'<\/span>;\n\n<span class=\"hljs-comment\">\/\/ show the attributes<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $article-&gt;class . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ light<\/span>\n<span class=\"hljs-keyword\">echo<\/span> $article-&gt;id . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>; <span class=\"hljs-comment\">\/\/ main<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEh0bWxFbGVtZW50CnsKCXByaXZhdGUgJGF0dHJpYnV0ZXMgPSBbXTsKCglwcml2YXRlICR0YWc7CgoJcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCR0YWcpCgl7CgkJJHRoaXMtPnRhZyA9ICR0YWc7Cgl9CgoJcHVibGljIGZ1bmN0aW9uIF9fc2V0KCRuYW1lLCAkdmFsdWUpCgl7CgkJJHRoaXMtPmF0dHJpYnV0ZXNbJG5hbWVdID0gJHZhbHVlOwoJfQoKCXB1YmxpYyBmdW5jdGlvbiBfX2dldCgkbmFtZSkKCXsKCQlpZiAoYXJyYXlfa2V5X2V4aXN0cygkbmFtZSwgJHRoaXMtPmF0dHJpYnV0ZXMpKSB7CgkJCXJldHVybiAkdGhpcy0-YXR0cmlidXRlc1skbmFtZV07CgkJfQoJfQoKCXB1YmxpYyBmdW5jdGlvbiBodG1sKCRpbm5lckhUTUwgPSAnJykKCXsKCQkkaHRtbCA9ICI8eyR0aGlzLT50YWd9IjsKCQlmb3JlYWNoICgkdGhpcy0-YXR0cmlidXRlcyBhcyAka2V5ID0-ICR2YWx1ZSkgewoJCQkkaHRtbCAuPSAnICcgLiAka2V5IC4gJz0iJyAuICR2YWx1ZSAuICciJzsKCQl9CgkJJGh0bWwgLj0gJz4nOwoJCSRodG1sIC49ICRpbm5lckhUTUw7CgkJJGh0bWwgLj0gIjwvJHRoaXMtPnRhZz4iOwoKCQlyZXR1cm4gJGh0bWw7Cgl9Cn0KCiRhcnRpY2xlID0gbmV3IEh0bWxFbGVtZW50KCdhcnRpY2xlJyk7CgokYXJ0aWNsZS0-aWQgPSAnbWFpbic7CiRhcnRpY2xlLT5jbGFzcyA9ICdsaWdodCc7CgovLyBzaG93IHRoZSBhdHRyaWJ1dGVzCmVjaG8gJGFydGljbGUtPmNsYXNzIC4gJzxicj4nOyAvLyBsaWdodAplY2hvICRhcnRpY2xlLT5pZCAuICc8YnI-JzsgLy8gbWFpbg\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP Magic methods start with double underscores (__).<\/li>\n\n\n\n<li>PHP calls the <code>__get()<\/code> method automatically when you access a non-existing or inaccessible property.<\/li>\n\n\n\n<li>PHP calls the <code>__set()<\/code> method automatically when you assign a value to a non-existing or inaccessible property.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"95\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-magic-methods\/\"\n\t\t\t\tdata-post-title=\"PHP Magic Methods\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"95\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-magic-methods\/\"\n\t\t\t\tdata-post-title=\"PHP Magic Methods\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about PHP magic methods that override the default actions when the object performs the actions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-95","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/95\/revisions"}],"predecessor-version":[{"id":3235,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/95\/revisions\/3235"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}