Home Backend Development PHP Tutorial PHP constructor method calling problem when creating class instance_PHP tutorial

PHP constructor method calling problem when creating class instance_PHP tutorial

Jul 21, 2016 pm 02:54 PM
php create Example method yes of kind transfer question

This article talks about the problem of calling the constructor method when PHP creates an instance of a class.

Unlike Java in PHP, when creating an instance of a class, the constructor of the parent class (super class) will be automatically called first to ensure that all attributes can be initialized correctly. PHP will not automatically call the constructor of the parent class in the constructor of this class. If you really need to call the parent class's constructor, you can manually call parent::__construct($params...);

class Base{
function __construct(){
//do sth here....
}
}

If we need an Extender to inherit Base, what we need to pay attention to is

1. Do we need to call the parent class? The constructor is used for initialization

2. Do we need to perform other extension operations based on the parent class constructor?

3. We do not need the initialization actions in the Base constructor .

If our Extender only satisfies situation 1, then we can omit the Extender's constructor, because the parent class's constructor fully meets our requirements, and we do not need to perform additional coding.

If our Extender satisfies situation 2, then we must first call parent::__construct() in the Extender's constructor, initialize it first, and then perform some extension operations

If our Extender satisfies In case 3, we can choose not to call the constructor of the parent class. There is just one flaw in this: when we expand Extender, if the extended class requires the initialization operation of the Base constructor, calling parent::__construct() at this time can only call the constructor of Extender but not the constructor of the Base class. Method.

Therefore, I personally think that it is better to manually call the constructor of the parent class in the constructor of the subclass.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364611.htmlTechArticleThis article talks about the problem of calling the constructor method when PHP creates an instance of a class. In PHP, unlike Java, when creating an instance of a class, the parent class (super...
will be automatically called first)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles