构造函数是PHP5 OOP(面向对象编程)的概念。构造函数与我们在程序中声明的类相关联。构造函数是在类的对象实例化时自动调用的,因此构造函数的定义是这样的:“构造函数是一种特殊的方法,在类的对象实例化时自动调用。”在本主题中,我们将学习 PHP 中的构造函数。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法
<?php Class Classname { //constructor function __construct() { //statements } } ?>
在上面的代码中,构造函数以 __ 双下划线开头,后跟关键字 constructor。没有 __construct() 的构造函数或者如果类的名称发生更改,则定义的方法只是一个方法而不是构造函数。因此,根据定义的语法在类中定义构造函数非常重要。
构造函数的类型
以下是一些类型的构造函数,其输出如下
1) 预定义构造函数
为了澄清预定义构造函数,让我们看一下下面给出的示例
预定义构造函数示例
代码:
<?php class Person { function CanSpeak() { echo " Not a constructor method " . '<br>'; } function __construct() { echo " In the constructor method " . '<br>'; } } //Object of class calling the constructor internally $p = new Person(); // Object of class calling the normal method $p->CanSpeak(); ?>
输出:
在上面的例子中
我们有一个类 Person,它有两个方法,其中一个 Person CanSpeak() 方法和构造函数方法 __construct()。接下来,我们将该类实例化为对象 $p。使用 $p 我们调用了普通方法。一旦创建了对象,就会调用构造函数方法并执行方法内的语句,类似地,使用相同的对象 $p 和内部语句调用 CanSpeak() 方法,该方法是普通方法而不是构造函数方法该方法被执行。另外,由于上面定义的构造函数没有任何参数,我们将其称为零参数构造函数或预定义构造函数。
2) 参数化构造函数
构造函数可以带有或不带有参数。带参数的构造函数称为参数化构造函数,不带参数的构造函数称为零参数构造函数。让我们看一个例子。
参数化构造函数示例
代码:
class Person { private $first; private $email; private $mobile; public function __construct($name, $email, $mobile) { echo "Initialising the object...<br>"; $this->name = $name; $this->email = $email; $this->mobile = $mobile; } public function showProfile() { echo "My name is: " . $this->name. " " . $this->email. " " . $this->mobile; } } $john = new Person("John","[email protected]", "9187986786"); $john->showProfile();
输出:
构造函数如何工作?
让我们看一下基类构造函数和派生类,派生类使用 extends 关键字扩展基类,派生类有自己的构造函数要执行,并且还要执行父构造函数。到目前为止,我们只了解了类中声明的构造函数。这里让我们为构造函数补充一些知识。在下面的例子中,基类Person有一个构造函数,现在这个构造函数被派生类或子类使用parent关键字调用,从而可以访问基类Person的构造函数。
输出流程
首先,调用 Person 构造函数,然后调用 Customer 构造函数,该构造函数在内部再次调用 Person 构造函数,然后调用其自己的客户构造函数,最后调用扩展 Person 类的 Employee 类,从而再次调用 Person 构造函数。
代码:
class Person { function __construct() { echo "In Person constructor"."<br>"; } } class Customer extends Person { function __construct() { parent::__construct(); echo "In Customer constructor"."<br>"; } } class Employee extends Person { // inherits Person’s constructor } // In Person constructor $p = new Person(); // In Person constructor // In Customer constructor $c = new Customer(); // In Employee constructor $e = new Employee();
输出:
在下面的示例中,我们展示了 set 方法和 get 方法的工作原理。使用 OOP 中的封装概念。最初,程序是用构造函数、set_name 方法和 get_name 方法声明的。请注意,构造函数是一个参数化构造函数,在实例化类时肯定会调用它,因此第一个输出是 John Doe 接下来创建了该类的对象,并调用了 set_name 和 get_name 方法,将输出打印为 Alice。
代码:
<?php class Person { public $name; function __construct($name) { echo $this->name = $name; } function set_name($name) { $this->name = $name; } function get_name() { echo $this->name; } } // In Person constructor $p = new Person('John Doe'); echo "\n"; $p->set_name('Alice'); $p->get_name(); ?>
输出:
访问说明符
PHP 中有三个访问说明符
- 公开
- 受保护
- 私人
公共:声明为公共的类的成员可以在任何地方访问。
Protected: Members of the class declared as protected are accessible only within the base class and the derived class which extends the base class.
Private: Members of the class declared as private are accessible with the class that defines it.
Also, the variables declared are called data members or properties and the functions declared are called as data methods. In the below example we have Base Class declared as Person which has the following properties along with the access specifiers public name, protected email and private mobile. Now the class is instantiated with an object $p and these three properties which are accessed from the object. which outputs are an error, why because, the protected property says that protected are accessible only within the base class and the derived class which extends the base class?
Code:
<?php class Person { public $name=; protected $email; private $mobile; function __construct() { print "In Person constructor"; } } // In Person constructor $p = new Person(); echo $p->name; echo $p->email; echo $p->mobile; ?>
Output:
Conclusion
Hope this article finds you what you have been searching for. The article has different examples for you to learn. The more you put the examples in practice the easier it will become to grasp.
以上是PHP 中的构造函数的详细内容。更多信息请关注PHP中文网其他相关文章!

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInacookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionwwithSession_start()和stordoredAtain $ _session.2)

在PHP中,遍历会话数据可以通过以下步骤实现:1.使用session_start()启动会话。2.通过foreach循环遍历$_SESSION数组中的所有键值对。3.处理复杂数据结构时,使用is_array()或is_object()函数,并用print_r()输出详细信息。4.优化遍历时,可采用分页处理,避免一次性处理大量数据。这将帮助你在实际项目中更有效地管理和使用PHP会话数据。

会话通过服务器端的状态管理机制实现用户认证。1)会话创建并生成唯一ID,2)ID通过cookies传递,3)服务器存储并通过ID访问会话数据,4)实现用户认证和状态管理,提升应用安全性和用户体验。

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

PHPSession失效的原因包括配置错误、Cookie问题和Session过期。1.配置错误:检查并设置正确的session.save_path。2.Cookie问题:确保Cookie设置正确。3.Session过期:调整session.gc_maxlifetime值以延长会话时间。

在PHP中调试会话问题的方法包括:1.检查会话是否正确启动;2.验证会话ID的传递;3.检查会话数据的存储和读取;4.查看服务器配置。通过输出会话ID和数据、查看会话文件内容等方法,可以有效诊断和解决会话相关的问题。

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

在PHP中配置会话生命周期可以通过设置session.gc_maxlifetime和session.cookie_lifetime来实现。1)session.gc_maxlifetime控制服务器端会话数据的存活时间,2)session.cookie_lifetime控制客户端cookie的生命周期,设置为0时cookie在浏览器关闭时过期。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具