Table of Contents
php构造方法中析构方法在继承中的表现,php构造
您可能感兴趣的文章:
Home php教程 php手册 php构造方法中析构方法在继承中的表现,php构造

php构造方法中析构方法在继承中的表现,php构造

Jun 13, 2016 am 08:42 AM
php Construction method Destructor method inherit

php构造方法中析构方法在继承中的表现,php构造

本文为大家分享了php构造方法之析构方法在继承中的表现,供大家参考,具体内容如下

子类中没有定义构造方法时,会自动调用父类的构造方法。因此实例化子类时,需按照父类的构造方法的形式进行。

修改为:

子类定义了自己的构造方法,则不会自动调用父类的构造方法,但可以手动调用:parent::__construct();

但通常,在子类,很多时候,在构造方法中,都应该(需要)去调用父类的构造方法以节省代码,增加可读性:

子类中没有定义析构方法时,会自动调用父类的析构方法。子类定义了自己的析构方法,则不会自动调用父类的析构方法,但可以手动调用:parent::__destruct()。重写override

重写又叫覆盖,就是将从父类继承下来的属性或方法重新“定义”——就是从新写。

但注意:子类覆盖父类的方法,虽然可以去调用父类的同名方法去完成一定的工作,但不是必须的。也有可能父类的该方法所执行的结果并不适合子类,此时子类就去完全自己写了。

重写的基本要求:

访问控制权限:下级的访问控制权限应该不低于上级的访问控制权限:上级:public 下级:只能public上级:protected 下级: protected, public上级:private 下级:private protected public——实际此情况无意义。私有的不能覆盖,而是完全当作自己全新的。

方法的参数形式:应该跟父类的一致。

私有属性和私有方法的重写问题:私有属性和方法都不能覆盖,但其实子类可以定义跟父类私有的同名属性或方法。只是当作一个自身的新的属性或方法来看待而已。不过方法的参数必须一致。构造方法的重写问题:构造方法不但可以像其他普通方法一样重写,而且,比普通方法更宽松:重写的时候参数可以不一致。

最终类final class:

通常,一个类,没有特别声明的话,则“别人”就可以随意拿过来使用并对之进行“扩展”——继承。

但是:

如果某个类不希望对其进行扩展,则可以将其声明为“最终类”。

形式:

final class 类名{ 。。。。类定义。。。。}

最终方法final method

通常,一个方法,如果没有特别声明,则下级类就可以对其进行“覆盖”(重写)。

但是:

如果某个方法不希望被下级类覆盖,就可以对其生命为“最终方法”。

形式:

final function 方法名(){。。。。方法定义。。。。}

以上就是本文的全部内容,希望对大家的学习有所帮助。

您可能感兴趣的文章:

  • php面向对象全攻略 (四)构造方法与析构方法
  • php学习笔记 面向对象的构造与析构方法
  • PHP的构造方法,析构方法和this关键字详细介绍
  • 析构函数与php的垃圾回收机制详解
  • php析构函数的具体用法小结
  • php初始化对象和析构函数的简单实例
  • PHP中构造函数和析构函数解析
  • php实现parent调用父类的构造方法与被覆写的方法
  • php析构函数的简单使用说明
  • PHP类的声明与实例化及构造方法与析构方法详解
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles