{"id":643,"date":"2021-03-24T07:37:53","date_gmt":"2021-03-24T07:37:53","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=643"},"modified":"2025-04-07T11:23:41","modified_gmt":"2025-04-07T11:23:41","slug":"php-call-parent-constructor","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-call-parent-constructor\/","title":{"rendered":"How to Call the Parent Constructor"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to call the parent constructor from the constructor of the child class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='child-class-doesnt-have-a-constructor'>Child class doesn&#8217;t have a constructor <a href=\"#child-class-doesnt-have-a-constructor\" class=\"anchor\" id=\"child-class-doesnt-have-a-constructor\" title=\"Anchor for Child class doesn&#039;t have a constructor\">#<\/a><\/h2>\n\n\n\n<p><a href=\"https:\/\/phptutorial.net\/php-oop\/php-inheritance\/\">In the inheritance tutorial<\/a>, you have learned how to define the <code>SavingAccount<\/code> class that inherits the <code>BankAccount<\/code> class:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/php-inheritance.svg\" alt=\"php inheritance\" class=\"wp-image-656\"\/><\/figure>\n<\/div>\n\n\n<p>However, we haven&#8217;t discussed the <a href=\"https:\/\/phptutorial.net\/php-oop\/php-constructors\/\">constructors<\/a> of the parent and child classes in the context of inheritance.<\/p>\n\n\n\n<p>The following adds a constructor to the <code>BankAccount<\/code> class, which accepts the <code>$balance<\/code> parameter. The constructor assigns the <code>$balance<\/code> argument to the <code>$balance<\/code> property:<\/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-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $balance;\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\">($balance)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance = $balance;\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\">getBalance<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;balance;\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\">deposit<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span>) {\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance += $amount;\n\t\t}\n\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>;\n\t}\n}<\/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>SavingAccount<\/code> class remains the same and doesn&#8217;t include its own constructor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SavingAccount<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $interestRate;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">setInterestRate<\/span><span class=\"hljs-params\">($interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">addInterest<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-comment\">\/\/ calculate interest<\/span>\n\t\t$interest = <span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate * <span class=\"hljs-keyword\">$this<\/span>-&gt;getBalance();\n\t\t<span class=\"hljs-comment\">\/\/ deposit interest to the balance<\/span>\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;deposit($interest);\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>When you create a new instance of the <code>SavingAccount<\/code>, PHP will call the constructor of the <code>SavingAccount<\/code> class. However, PHP cannot find the constructor in the <code>SavingAccount class<\/code>. Therefore, it continues to search for the constructor of the parent class of the <code>SavingAccount<\/code> class, which is the <code>BankAccount<\/code> class. And it invokes the constructor of the <code>BankAccount<\/code> class.<\/p>\n\n\n\n<p>If you don&#8217;t pass an argument to the constructor of the <code>SavingAccount<\/code> class, you&#8217;ll get an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$account = <span class=\"hljs-keyword\">new<\/span> SavingAccount();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Fatal error: Uncaught ArgumentCountError: Too few arguments to <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">BankAccount<\/span>::<span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span>, 0 <span class=\"hljs-title\">passed<\/span> <span class=\"hljs-title\">in<\/span> ... <span class=\"hljs-title\">on<\/span> <span class=\"hljs-title\">line<\/span> 5 <span class=\"hljs-title\">and<\/span> <span class=\"hljs-title\">exactly<\/span> 1 <span class=\"hljs-title\">expected<\/span> <span class=\"hljs-title\">in<\/span> ...<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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>But if you pass an argument to the constructor, it&#8217;ll work perfectly:<\/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\">$account = <span class=\"hljs-keyword\">new<\/span> SavingAccount(<span class=\"hljs-number\">100<\/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<h2 class=\"wp-block-heading\" id='define-a-constructor-in-the-child-class'>Define a constructor in the child class <a href=\"#define-a-constructor-in-the-child-class\" class=\"anchor\" id=\"define-a-constructor-in-the-child-class\" title=\"Anchor for Define a constructor in the child class\">#<\/a><\/h2>\n\n\n\n<p>A child class can have its own constructor. For example, you can add a constructor to the <code>SavingAccount<\/code> class as follows:<\/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-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SavingAccount<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $interestRate;\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\">($interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">setInterestRate<\/span><span class=\"hljs-params\">($interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">addInterest<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-comment\">\/\/ calculate interest<\/span>\n\t\t$interest = <span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate * <span class=\"hljs-keyword\">$this<\/span>-&gt;getBalance();\n\t\t<span class=\"hljs-comment\">\/\/ deposit interest to the balance<\/span>\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;deposit($interest);\n\t}\n}<\/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 <code>SavingAccount<\/code> class has a constructor that initializes the <code>interestRate<\/code> property.<\/p>\n\n\n\n<p>When a child class has its own constructor, the constructor in the child class will not call the constructor of its parent class automatically. <\/p>\n\n\n\n<p>For example, the following creates a new instance of the <code>SavingAccount<\/code> class and initializes the <code>$interestRate<\/code> property to the value <code>0.05<\/code> <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$account = <span class=\"hljs-keyword\">new<\/span> SavingAccount(<span class=\"hljs-number\">0.05<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>To call the constructor of the parent class from the constructor of the child class, you use the <code>parent::__construct(arguments)<\/code> syntax. <\/p>\n\n\n\n<p>The following changes the constructor of the <code>SavingAccount<\/code> class that accepts two arguments: balance &amp; interest rate. It also calls the constructor of the <code>BankAccount<\/code> class to initialize the <code>$balance<\/code> property:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SavingAccount<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $interestRate;\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\">($balance, $interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">parent<\/span>::__construct($balance);\n\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">setInterestRate<\/span><span class=\"hljs-params\">($interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">addInterest<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-comment\">\/\/ calculate interest<\/span>\n\t\t$interest = <span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate * <span class=\"hljs-keyword\">$this<\/span>-&gt;getBalance();\n\t\t<span class=\"hljs-comment\">\/\/ deposit interest to the balance<\/span>\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;deposit($interest);\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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 syntax for calling the parent constructor is the same as a regular method.<\/p>\n\n\n\n<p>Put it all together:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $balance;\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\">($balance)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance = $balance;\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\">getBalance<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;balance;\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\">deposit<\/span><span class=\"hljs-params\">($amount)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">if<\/span> ($amount &gt; <span class=\"hljs-number\">0<\/span>) {\n\t\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;balance += $amount;\n\t\t}\n\n\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>;\n\t}\n}\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SavingAccount<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">BankAccount<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $interestRate;\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\">($balance, $interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">parent<\/span>::__construct($balance);\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">setInterestRate<\/span><span class=\"hljs-params\">($interestRate)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate = $interestRate;\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\">addInterest<\/span><span class=\"hljs-params\">()<\/span>\n\t<\/span>{\n\t\t$interest = <span class=\"hljs-keyword\">$this<\/span>-&gt;interestRate * <span class=\"hljs-keyword\">$this<\/span>-&gt;getBalance();\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;deposit($interest);\n\t}\n}\n\n$account = <span class=\"hljs-keyword\">new<\/span> SavingAccount(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">0.05<\/span>);\n$account-&gt;addInterest();\n<span class=\"hljs-keyword\">echo<\/span> $account-&gt;getBalance();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCmNsYXNzIEJhbmtBY2NvdW50CnsKCXByaXZhdGUgJGJhbGFuY2U7CgoJcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCRiYWxhbmNlKQoJewoJCSR0aGlzLT5iYWxhbmNlID0gJGJhbGFuY2U7Cgl9CgoJcHVibGljIGZ1bmN0aW9uIGdldEJhbGFuY2UoKQoJewoJCXJldHVybiAkdGhpcy0-YmFsYW5jZTsKCX0KCglwdWJsaWMgZnVuY3Rpb24gZGVwb3NpdCgkYW1vdW50KQoJewoJCWlmICgkYW1vdW50ID4gMCkgewoJCQkkdGhpcy0-YmFsYW5jZSArPSAkYW1vdW50OwoJCX0KCgkJcmV0dXJuICR0aGlzOwoJfQp9CgpjbGFzcyBTYXZpbmdBY2NvdW50IGV4dGVuZHMgQmFua0FjY291bnQKewoJcHJpdmF0ZSAkaW50ZXJlc3RSYXRlOwoKCXB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdCgkYmFsYW5jZSwgJGludGVyZXN0UmF0ZSkKCXsKCQlwYXJlbnQ6Ol9fY29uc3RydWN0KCRiYWxhbmNlKTsKCQkkdGhpcy0-aW50ZXJlc3RSYXRlID0gJGludGVyZXN0UmF0ZTsKCX0KCglwdWJsaWMgZnVuY3Rpb24gc2V0SW50ZXJlc3RSYXRlKCRpbnRlcmVzdFJhdGUpCgl7CgkJJHRoaXMtPmludGVyZXN0UmF0ZSA9ICRpbnRlcmVzdFJhdGU7Cgl9CgoJcHVibGljIGZ1bmN0aW9uIGFkZEludGVyZXN0KCkKCXsKCQkkaW50ZXJlc3QgPSAkdGhpcy0-aW50ZXJlc3RSYXRlICogJHRoaXMtPmdldEJhbGFuY2UoKTsKCQkkdGhpcy0-ZGVwb3NpdCgkaW50ZXJlc3QpOwoJfQp9CgokYWNjb3VudCA9IG5ldyBTYXZpbmdBY2NvdW50KDEwMCwgMC4wNSk7CiRhY2NvdW50LT5hZGRJbnRlcmVzdCgpOwplY2hvICRhY2NvdW50LT5nZXRCYWxhbmNlKCk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The following class diagram illustrates inheritance between the <code>SavingAccount<\/code> and <code>BankAccount<\/code> classes:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/03\/PHP-parent-construct.svg\" alt=\"\" class=\"wp-image-657\"\/><\/figure>\n<\/div>\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>The constructor of the child class doesn&#8217;t automatically call the constructor of its parent class.<\/li>\n\n\n\n<li>Use <code>parent::__construct(arguments)<\/code> to call the parent constructor from the constructor in the child class.<\/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=\"643\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-call-parent-constructor\/\"\n\t\t\t\tdata-post-title=\"How to Call the Parent Constructor\"\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=\"643\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-call-parent-constructor\/\"\n\t\t\t\tdata-post-title=\"How to Call the Parent Constructor\"\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&#8217;ll learn how to call the parent constructor from the constructor of the child class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-643","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/643","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=643"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/643\/revisions"}],"predecessor-version":[{"id":3227,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/643\/revisions\/3227"}],"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=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}