PHP Object-oriented constructor description

Source: Internet
Author: User
Tags php class

This article does not repeat the object-oriented knowledge, this article focuses on the PHP constructor.
The constructor of a PHP class can be a magic cube __construct () or a function with the same name as the class, as in the following example:

  class A{     Public  function A(){        Echo ' A is constructing ... '; }  } class B{     Public  function __construct(){        Echo ' B is contructing ... '; } }$a=NewA ();//A is constructing ... $b=NewB ();//B is constructing ...

In addition, when inheriting, it should be noted that:
" subclasses can not write constructors, then use the constructor of the parent class "

  class A{    protected $name; Public  function A(){        Echo ' A is constructing...<br> '; } Public  function set_name($name){        $this->name =$name; } Public  function get_name(){        return $this->name; } } class B extends A{    /* Public Function __construct () {echo ' B is contructing...<br> '; }    */}//$a = new A (); $b=NewB ();//A is constructing ... $b->set_name (' Zhangsan ');Echo $b->get_name ();

" if the subclass writes a constructor, the constructor for the parent class is no longer called "

  class A{    protected $name; Public  function A(){        Echo ' A is constructing...<br> '; } Public  function set_name($name){        $this->name =$name; } Public  function get_name(){        return $this->name; } } class B extends A{     Public  function __construct(){        Echo ' B is contructing...<br> '; } }//$a = new A (); $b=NewB ();//Just echo ' B is contructing ... ' $b->set_name (' Zhangsan ');Echo $b->get_name ();//Zhangsan

" if the constructor of the parent class is private, it can be inherited, but the subclass must have its own constructor and explicitly write "

  class A{    protected $name;Private  function A(){        Echo ' A is constructing...<br> '; } Public  function set_name($name){        $this->name =$name; } Public  function get_name(){        return $this->name; } } class B extends A{     Public  function __construct(){        Echo ' B is contructing...<br> '; } }//$a = new A (); $b=NewB ();//B is contructing ... $b->set_name (' Zhangsan ');Echo $b->get_name ();//Zhangsan

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Object-oriented constructor description

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.