Answers for "php parent constructor"

PHP
6

php contruct parent

<?php
class BaseClass {
    function __construct() {
        print "In BaseClass constructor\n";
    }
}

class SubClass extends BaseClass {
    function __construct() {
        parent::__construct();
        print "In SubClass constructor\n";
    }
}

class OtherSubClass extends BaseClass {
    // inherits BaseClass's constructor
}

// In BaseClass constructor
$obj = new BaseClass();

// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();

// In BaseClass constructor
$obj = new OtherSubClass();
?>
Posted by: Guest on February-22-2020
1

php call parent constructor

<?php
class BaseClass {
   function __construct() {
       print "En el constructor BaseClass\n";
   }
}

class SubClass extends BaseClass {
   function __construct() {
       parent::__construct();
       print "En el constructor SubClass\n";
   }
}
Posted by: Guest on April-12-2021
1

if want to call parent class constructor in php

<?php
/**
 *Inheritance:  It means accessing property of parent class from child class.
 * 
 * */
class Animal
{
    function __construct()
    {
        echo '<br>construct1';
    }
    function fun1() 
    {
        echo '<br>fun1';
    }
}
class Dog extends Animal
{
    function __construct()
    {
        // if want to call parent class constructor 
        parent::__construct();
        echo '<br>construct2';
    }
    function fun1()
    {
        echo '<br>Dog class function called';
    } 
}
$obj = new Dog();
$obj ->fun1();
?>
Posted by: Guest on October-05-2021

Code answers related to "php parent constructor"

Browse Popular Code Answers by Language