首页 >后端开发 >php教程 >PHP 中的构造函数

PHP 中的构造函数

PHPz
PHPz转载
2024-02-28 08:22:57483浏览

php小编小新为您详细解析PHP中的构造函数。构造函数是在实例化对象时自动调用的方法,用于初始化对象的属性。通过构造函数,可以在创建对象时传递参数并对属性进行赋值,提高代码的可读性和重用性。构造函数的命名与类名相同,且不需要手动调用,便于统一管理和维护代码。在PHP中,构造函数是面向对象编程的重要组成部分,深入了解并合理应用构造函数,能够提升代码的质量和效率。

我们还将使用该函数来初始化类中具有给定参数的对象的属性。

最后,我们将看到如何在子类中启动对象并在两个类都有单独的构造函数时调用父类构造函数。


使用 php 构造函数初始化类中的对象的属性

在下面的示例中,我们将创建一个类 Student 并使用 __construct 函数为 new Student 分配其属性。

__construct 函数减少了与使用函数 set_name() 相关的代码数量。

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Student</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#408080;font-style:italic">// Define the attributes of your class
</span></span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$name</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$em<strong class="keylink">ai</strong>l</span>;
</span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic">// Initialize the properties of the object you want to create in this class
</span></span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">funct<strong class="keylink">io</strong>n</span> __construct(<span style="color:#19177c">$name</span>, <span style="color:#19177c">$email</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span> <span style="color:#666">=</span> <span style="color:#19177c">$name</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">email</span> <span style="color:#666">=</span> <span style="color:#19177c">$email</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">get_name</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">return</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">get_email</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">return</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">email</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#19177c">$obj</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Student(<span style="color:#ba2121">"John"</span>, <span style="color:#ba2121">"john567@gmail.com"</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$obj</span><span style="color:#666">-></span><span style="color:#7d9029">get_name</span>();
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"<br>"</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$obj</span><span style="color:#666">-></span><span style="color:#7d9029">get_email</span>();
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

输出:

<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>John
</span></span><span style="display:flex;"><span>john567@gmail.com
</span></span></code></code>

使用 PHP 构造函数在类中初始化 Object with Parameters 的属性

在下面的示例代码中,我们创建类 Military 并使用 __construct 函数来提供我们创建的对象的属性和参数。

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Military</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#408080;font-style:italic">// Define the attributes of the class &#39;Military&#39;
</span></span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$name</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$rank</span>;
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">function</span> __construct(<span style="color:#19177c">$name</span>, <span style="color:#19177c">$rank</span>){
</span></span><span style="display:flex;"><span>		<span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span> <span style="color:#666">=</span> <span style="color:#19177c">$name</span>;
</span></span><span style="display:flex;"><span>		<span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">rank</span> <span style="color:#666">=</span> <span style="color:#19177c">$rank</span>;
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">show_detail</span>() {
</span></span><span style="display:flex;"><span>		<span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span><span style="color:#666">.</span><span style="color:#ba2121">" : "</span>;
</span></span><span style="display:flex;"><span>		<span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Your Rank is "</span><span style="color:#666">.</span><span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">rank</span><span style="color:#666">.</span><span style="color:#ba2121">"</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#ba2121">"</span>;
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#19177c">$person_obj</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Military(<span style="color:#ba2121">"Michael"</span>, <span style="color:#ba2121">"General"</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$person_obj</span><span style="color:#666">-></span><span style="color:#7d9029">show_detail</span>();
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"<br>"</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$person2</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Military(<span style="color:#ba2121">"Fred"</span>, <span style="color:#ba2121">"Commander"</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$person2</span><span style="color:#666">-></span><span style="color:#7d9029">show_detail</span>();
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

输出:

<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Michael : Your Rank is General
</span></span><span style="display:flex;"><span>Fred : Your Rank is Commander
</span></span></code></code>

在 PHP 中在子类中启动一个对象并在两个类都有 Individual 构造函数时调用父类构造函数

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Student</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$name</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">function</span> __construct(<span style="color:#19177c">$name</span>)
</span></span><span style="display:flex;"><span>	{
</span></span><span style="display:flex;"><span>		<span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span> <span style="color:#666">=</span> <span style="color:#19177c">$name</span>;
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}<span style="color:#008000;font-weight:bold">class</span> <span style="color:#00f;font-weight:bold">Identity</span> <span style="color:#008000;font-weight:bold">extends</span> Student
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#19177c">$identity_id</span>;
</span></span><span style="display:flex;"><span>	
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">public</span> <span style="color:#008000;font-weight:bold">function</span> __construct(<span style="color:#19177c">$name</span>, <span style="color:#19177c">$identity_id</span>)
</span></span><span style="display:flex;"><span>	{
</span></span><span style="display:flex;"><span>		<span style="color:#008000;font-weight:bold">parent</span><span style="color:#666">::</span><span style="color:#7d9029">__construct</span>(<span style="color:#19177c">$name</span>);
</span></span><span style="display:flex;"><span>		<span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">identity_id</span> <span style="color:#666">=</span> <span style="color:#19177c">$identity_id</span>;
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#008000;font-weight:bold">function</span> <span style="color:#00f">show_detail</span>() {
</span></span><span style="display:flex;"><span>		<span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">name</span><span style="color:#666">.</span><span style="color:#ba2121">" : "</span>;
</span></span><span style="display:flex;"><span>		<span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">"Your Id Number is "</span><span style="color:#666">.</span><span style="color:#19177c">$this</span><span style="color:#666">-></span><span style="color:#7d9029">identity_id</span><span style="color:#666">.</span><span style="color:#ba2121">"</span><span style="color:#b62;font-weight:bold">\n</span><span style="color:#ba2121">"</span>;
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#19177c">$obj</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Identity(<span style="color:#ba2121">'Alice'</span>, <span style="color:#ba2121">'1036398'</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$obj</span><span style="color:#666">-></span><span style="color:#7d9029">show_detail</span>();
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

输出:

<code>
<code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>Alice : Your Id Number is 1036398
</span></span></code></code>

Identity 类扩展了上述代码中的 Student 类。我们使用关键字 parent: 来调用 Student 类的构造函数。

以上是PHP 中的构造函数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:lsjlt.com。如有侵权,请联系admin@php.cn删除