search
Homephp教程php手册php基础知识:类与对象(3) 构造函数和析构函数

php基础知识:类与对象(3) 构造函数和析构函数

Jun 13, 2016 pm 12:35 PM
phpphp5andfunctionandexistbasic knowledgedefinitionobjectDevelopermethoddestructkind

构造函数
PHP 5 允行开发者在一个类中定义一个方法作为构造函数。具有构造函数的类会在每次创建对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作。 
注意: 
如果子类中定义了构造函数则不会暗中调用其父类的构造函数。要执行父类的构造函数,需要在子类的构造函数中调用 parent::__construct()。(??和其他语言明显不同??)

例10.8.使用新标准的构造函数
class BaseClass {
  function __construct() {
      print "In BaseClass constructor\n";
  }
}

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

为了实现向后兼容性,如果 PHP 5 在类中找不到 __construct() 函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数。因此唯一会产生兼容性问题的情况是:类中已有一个名为 __construct() 的方法,但它却又不是构造函数。 

析构函数
PHP 5 引入了析构函数的概念,这类似于其它面向对象的语言,如 C++。析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。 
例10.9.析构函数示例
class MyDestructableClass {
  function __construct() {
      print "In constructor\n";
      $this->name = "MyDestructableClass";
  }

  function __destruct() {
      print "Destroying " . $this->name . "\n";
  }
}
$obj = new MyDestructableClass();
和构造函数一样,父类的析构函数不会被引擎暗中调用。要执行父类的析构函数,必须在子类的析构函数体中显式调用 parent::__destruct()。 (??和其他语言明显不同??)
注意: 
析构函数在脚本关闭时调用,此时所有的头信息已经发出。 
注意: 
试图在析构函数中抛出一个异常会导致致命错误。

Statement
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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function