PHP follow constructor, member variable

Source: Internet
Author: User
PHP inherits the constructor method, and the member variables are often vague about these concepts, which are summarized as follows :? 1. if the subclass does not have a constructor, the constructor of the parent class is called by default? 2. if the subclass has a constructor, the constructor of the parent class is not called and the member variables of the parent class are overwritten. if you want to call the construct ();? 3. Will the member variables and methods of the parent class inherit from the quilt class by default? Eg: c PHP inherited constructor, member variable

I found myself vague about these concepts, which are summarized as follows:

?

1. if the subclass does not have a constructor, the constructor of the parent class is called by default.

?

2. if the subclass has a constructor, the constructor of the parent class will not be called, and the member variables of the parent class will be overwritten. if you want to call the construct (), use parent :__ construct ();

?

3. the member variables and methods of the parent class inherit from the quilt class by default.

?

Eg:

    class A{        public $a = array();        public $b = '';        public function __construct(){            $this->a = array('a', 'b', 'c');            $this->b = 'bobby';            echo 'construct' . "\n";        }        public function get(){            print_r($this->a);        }    }    class B extends A{        public function __construct(){            parent::__construct();            $this->c = 'CC';        }        public function get(){            array_push($this->a, 'd');            print_r($this->a);            echo $this->c;        }        public function getb(){            echo parent::$this->b;        }    }    //$a = new A();    $b = new B();    $b->get();    $b->getb();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.