PHP面向对象——构造函数、析构函数
我们在创建和销毁对象时需要执行一些任务。例如,在创建对象时给属性赋值,在对象销毁时关闭数据连接等,这时就需要构造函数和析构函数。
在PHP中构造函数和析构函数是固定的,如下:
<font face="微软雅黑"><span style="color: #008000">//</span><span style="color: #008000"> 构造函数</span> <span style="color: #0000ff">function</span> __construct([argument1,argument2,...,</font><font face="微软雅黑"><span style="color: #000000">argumentN]){ </span><span style="color: #008000">/*</span><span style="color: #008000"> Class initialization code </span><span style="color: #008000">*/</span></font><span style="color: #000000"> <font face="微软雅黑">} </font></span><font face="微软雅黑"><span style="color: #008000">//</span><span style="color: #008000"> 析构函数</span> <span style="color: #0000ff">function</span></font><font face="微软雅黑"><span style="color: #000000"> __destruct(){ </span><span style="color: #008000">//</span><span style="color: #008000"> ...</span> }</font>
构造函数
- 构造函数可以接受参数,能够在创建对象时赋值给对象属性
- 构造函数可以调用类方法或其他函数
- 构造函数可以调用其他类的构造函数
构造函数使用例子:
<span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> Person{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$age</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$gender</span><span style="color: #000000">; </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$age</span>,<span style="color: #800080">$gender</span><span style="color: #000000">){ </span><span style="color: #800080">$this</span>->setName(<span style="color: #800080">$name</span><span style="color: #000000">); </span><span style="color: #800080">$this</span>->setAge(<span style="color: #800080">$age</span><span style="color: #000000">); </span><span style="color: #800080">$this</span>->setGender(<span style="color: #800080">$gender</span><span style="color: #000000">); } </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> setName(<span style="color: #800080">$name</span><span style="color: #000000">){ </span><span style="color: #800080">$this</span>->name = <span style="color: #800080">$name</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> ... getter setter 方法</span> <span style="color: #000000"> } </span><span style="color: #800080">$peron</span> = <span style="color: #0000ff">new</span> Person("lee",18,'男'<span style="color: #000000">); </span>?>
调用父类构造函数、调用无关类的构造函数:
<span style="color: #0000ff">function</span><span style="color: #000000"> __construct(){ parent</span>::__construct(); <span style="color: #008000">//</span><span style="color: #008000"> 调用父类的构造函数必须显示的使用parent调用父类构造函数</span> classname::__construct(); <span style="color: #008000">//</span><span style="color: #008000"> 调用其他类的构造函数,classname是类名 //其他操作</span> }
继承和构造函数
PHP中的子类的构造函数不会主动调用父类的构造函数,要显示的使用parent::__construct()调用:
<span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> Animal{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span><span style="color: #000000">){ </span><span style="color: #800080">$this</span>->setName(<span style="color: #800080">$name</span><span style="color: #000000">) </span><span style="color: #0000ff">echo</span> "动物类被创建!"<span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> ... 其他方法</span> <span style="color: #000000"> } </span><span style="color: #0000ff">class</span> Birds <span style="color: #0000ff">extends</span><span style="color: #000000"> Animal{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$leg</span><span style="color: #000000">; </span><span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$leg</span><span style="color: #000000">){ parent</span>::__construct(<span style="color: #800080">$name</span>); <span style="color: #008000">//</span><span style="color: #008000"> 显示调用</span> <span style="color: #800080">$this</span>->setLeg(<span style="color: #800080">$leg</span><span style="color: #000000">); </span><span style="color: #0000ff">echo</span> "鸟类被创建!"<span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> ... 其他方法</span> <span style="color: #000000">} </span>?>
如果涉及多层继承,当调用parent::__construct()时,会沿着父类向上搜索,直到找到最合适的构造函数,例如:
<span style="color: #008000">//</span><span style="color: #008000"> 接上例</span> <span style="color: #0000ff">class</span> Parrot <span style="color: #0000ff">extends</span><span style="color: #000000"> Birds{ </span><span style="color: #0000ff">private</span> <span style="color: #800080">$name</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$leg</span><span style="color: #000000">; </span><span style="color: #0000ff">private</span> <span style="color: #800080">$wing</span><span style="color: #000000">; </span><span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span><span style="color: #000000">){ parent</span>::__construct(<span style="color: #800080">$name</span>); <span style="color: #008000">//</span><span style="color: #008000"> 此时没有找到父类(Birds类)合适的构造函数,只能向上搜索,搜索到Animal类时,才找到合适的构造函数</span> <span style="color: #0000ff">echo</span> "鹦鹉类被创建!"<span style="color: #000000">; </span><span style="color: #800080">$this</span>-><span style="color: #000000">smackTalk(); </span><span style="color: #008000">/*</span><span style="color: #008000"> 输出结果: "动物类被创建!" "鹦鹉说话!" </span><span style="color: #008000">*/</span><span style="color: #000000"> } </span><span style="color: #0000ff">function</span><span style="color: #000000"> smackTalk(){ </span><span style="color: #0000ff">echo</span> "鹦鹉说话!"<span style="color: #000000">; } }</span>
如果想要依次调用几个父类的构造函数,可以使用类名直接调用构造函数,例如:
<span style="color: #0000ff">function</span> __construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$leg</span><span style="color: #000000">){ Animal</span>::__construct(<span style="color: #800080">$name</span>); <span style="color: #008000">//</span><span style="color: #008000"> 调用Animal构造函数</span> Birds::__construct(<span style="color: #800080">$name</span>,<span style="color: #800080">$leg</span>); <span style="color: #008000">//</span><span style="color: #008000"> 调用Birds构造函数</span> }
PHP是不支持函数的重载,所有构造函数同样不能有多个。
析构函数
- 析构函数是在销毁对象时,自动调用,不能显示的调用。
- 析构函数不能带参数。
在以下几种情况下可能会调用析构函数(但不一定):
- PHP页面加载完毕之后;
- unset()类;
- 变量引用指向别的对象或值时;
PHP的内存回收机制和JAVA的很类似,对没有任何引用的对象进行销毁回收,采用引用计数器的技术。
例子:
<span style="color: #000000">php </span><span style="color: #0000ff">class</span><span style="color: #000000"> test{ </span><span style="color: #0000ff">function</span><span style="color: #000000"> __destruct(){ </span><span style="color: #0000ff">echo</span> "当对象销毁时会调用!!!"<span style="color: #000000">; } } </span><span style="color: #800080">$a</span> = <span style="color: #800080">$b</span> = <span style="color: #800080">$c</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> test(); </span><span style="color: #800080">$a</span> = <span style="color: #0000ff">null</span><span style="color: #000000">; </span><span style="color: #0000ff">unset</span>(<span style="color: #800080">$b</span><span style="color: #000000">); </span><span style="color: #0000ff">echo</span> "<hr>"<span style="color: #000000">; </span>?>
此例子,如下图,有三个变量引用$a,$b,$c指向test对象,test对象就有3个引用计数,当$a = null时,$a对test对象的引用丢失,计数-1,变为2,当$b被unset()时,$b对test对象的引用也丢失了,计数再-1,变为1,最后页面加载完毕,$c指向test对象的引用自动被释放,此时计数再-1,变为0,test对象已没有变量引用,就会被销毁,此时就会调用析构函数。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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.

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

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.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
