这篇文章主要介绍了关于PHP面向对象中的一些知识点,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1. __construct:
内置构造函数,在对象被创建时自动调用。见如下代码:
<?php class ConstructTest { private $arg1; private $arg2; public function __construct($arg1, $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } public function printAttributes() { print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n"; } } $testObject = new ConstructTest("arg1","arg2"); $testObject->printAttributes();
运行结果如下:
Stephens-Air:Desktop$ php Test.php __construct is called... $arg1 = arg1 $arg2 = arg2
2. parent:
用于在子类中直接调用父类中的方法,功能等同于Java中的super。
<?php class BaseClass { protected $arg1; protected $arg2; function __construct($arg1, $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } function getAttributes() { return '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2; } } class SubClass extends BaseClass { protected $arg3; function __construct($baseArg1, $baseArg2, $subArg3) { parent::__construct($baseArg1, $baseArg2); $this->arg3 = $subArg3; } function getAttributes() { return parent::getAttributes().' $arg3 = '.$this->arg3; } } $testObject = new SubClass("arg1","arg2","arg3"); print $testObject->getAttributes()."\n";
运行结果如下:
Stephens-Air:Desktop$ php Test.php __construct is called... $arg1 = arg1 $arg2 = arg2 $arg3 = arg3
3. self:
在类内调用该类静态成员和静态方法的前缀修饰,对于非静态成员变量和函数则使用this。
<?php class StaticExample { static public $arg1 = "Hello, This is static field.\n"; static public function sayHello() { print self::$arg1; } } print StaticExample::$arg1; StaticExample::sayHello();
运行结果如下:
Stephens-Air:Desktop$ php Test.php Hello, This is static field. Hello, This is static field.
4. static:
这里介绍的static关键字主要用于PHP 5.3以上版本新增的延迟静态绑定功能。请看一下代码和关键性注释。
<?php abstract class Base { public static function getInstance() { //这里的new static()实例化的是调用该静态方法的当前类。 return new static(); } abstract public function printSelf(); } class SubA extends Base { public function printSelf() { print "This is SubA::printSelf.\n"; } } class SubB extends Base { public function printSelf() { print "This is SubB::printSelf.\n"; } } SubA::getInstance()->printSelf(); SubB::getInstance()->printSelf();
运行结果如下:
Stephens-Air:Desktop$ php Test.php This is SubA::printSelf. This is SubB::printSelf.
static关键字不仅仅可以用于实例化。和self和parent一样,static还可以作为静态方法调用的标识符,甚至是从非静态上下文中调用。在该场景下,self仍然表示的是当前方法所在的类。见如下代码:
<?php abstract class Base { private $ownedGroup; public function __construct() { //这里的static和上面的例子一样,表示当前调用该方法的实际类。 //需要另外说明的是,这里的getGroup方法即便不是静态方法,也会得到相同的结果。然而倘若 //getGroup真的只是普通类方法,那么这里还是建议使用$this。 $this->ownedGroup = static::getGroup(); } public function printGroup() { print "My Group is ".$this->ownedGroup."\n"; } public static function getInstance() { return new static(); } public static function getGroup() { return "default"; } } class SubA extends Base { } class SubB extends Base { public static function getGroup() { return "SubB"; } } SubA::getInstance()->printGroup(); SubB::getInstance()->printGroup();
运行结果如下:
Stephens-Air:Desktop$ php Test.php My Group is default My Group is SubB
5. __destruct:
析构方法的作用和构造方法__construct刚好相反,它只是在对象被垃圾收集器收集之前自动调用,我们可以利用该方法做一些必要的清理工作。
<?php class TestClass { function __destruct() { print "TestClass destructor is called.\n"; } } $testObj = new TestClass(); unset($testObj); print "Application will exit.\n";
运行结果如下:
Stephens-Air:Desktop$ php Test.php TestClass destructor is called. Application will exit.
6. __clone:
在PHP 5之后的版本中,对象之间的赋值为引用赋值,即赋值后的两个对象将指向同一地址空间,如果想基于对象赋值,可以使用PHP提供的clone方法。该方法将当前对象浅拷贝之后的副本返回,如果想在clone的过程中完成一些特殊的操作,如深拷贝,则需要在当前类的声明中实现__clone方法,该方法在执行clone的过程中会被隐式调用。另外需要格外注意的是,__clone方法是作用再被拷贝的对象上,即赋值后的对象上执行。
<?php class InnerClass { public $id = 10; public function printSelf() { print '$id = '.$this->id."\n"; } } class OuterClass { public $innerClass; public function __construct() { $this->innerClass = new InnerClass(); } public function __clone() { $this->innerClass = clone $this->innerClass; print "__clone is called.\n"; } } $outerA = new OuterClass(); print "Before calling to clone.\n"; $outerB = clone $outerA; print "After calling to clone.\n"; $outerA->innerClass->id = 20; print "In outerA: "; $outerA->innerClass->printSelf(); print "In outerB: "; $outerB->innerClass->printSelf();
运行结果如下:
Stephens-Air:Desktop$ php Test.php Before calling to clone. __clone is called. After calling to clone. In outerA: $id = 20 In outerB: $id = 10
7. const:
PHP5可以在类中定义常量属性。和全局常量一样,一旦定义就不能改变。常量属性不需要像普通属性那样以$开头,按照惯例,只能用大写字母来命名常量。另外和静态属性一样,只能通过类而不能通过类的实例访问常量属性,引用常量时同样也不需要以$符号作为前导符。另外常量只能被赋值为基础类型,如整型,而不能指向任何对象类型。
<?php class TestClass { const AVAILABLE = 0; } print "TestClass::AVAILABLE = ".TestClass::AVAILABLE."\n";
运行结果如下:
0Stephens-Air:Desktop$ php Test.php TestClass::AVAILABLE = 0
相关推荐:
以上是关于PHP面向对象中一些方法、函数应用的代码的详细内容。更多信息请关注PHP中文网其他相关文章!

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

会话ID应在登录时、敏感操作前和每30分钟定期重新生成。1.登录时重新生成会话ID可防会话固定攻击。2.敏感操作前重新生成提高安全性。3.定期重新生成降低长期利用风险,但需权衡用户体验。

在PHP中设置会话cookie参数可以通过session_set_cookie_params()函数实现。1)使用该函数设置参数,如过期时间、路径、域名、安全标志等;2)调用session_start()使参数生效;3)根据需求动态调整参数,如用户登录状态;4)注意设置secure和httponly标志以提升安全性。

在PHP中使用会话的主要目的是维护用户在不同页面之间的状态。1)会话通过session_start()函数启动,创建唯一会话ID并存储在用户cookie中。2)会话数据保存在服务器上,允许在不同请求间传递数据,如登录状态和购物车内容。

如何在子域名间共享会话?通过设置通用域名的会话cookie实现。1.在服务器端设置会话cookie的域为.example.com。2.选择合适的会话存储方式,如内存、数据库或分布式缓存。3.通过cookie传递会话ID,服务器根据ID检索和更新会话数据。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中