- 时间:2021-02-10 13:03 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:PHP中static关键字以及与self关键字的区别
[b]概述[/b]
正在学习设计模式,之前有一篇文章关于[url=http://www.1sucai.cn/article/68752.htm]单例模式[/url]的文章,重新读了这篇文章,发现对static关键字掌握不是很牢靠,重新温习一下。
[b]static关键字[/b]
PHP手册里对static关键字的介绍如下:
[url=http://php.net/manual/zh/language.oop5.late-static-bindings.php]例子 [/url]
[b]self vs static[/b]
用一个demo来直接说明self与static的区别。
self示例:
<?php
class Vehicle {
protected static $name = 'This is a Vehicle';
public static function what_vehicle() {
echo get_called_class()."\n";
echo self::$name;
}
}
class Sedan extends Vehicle {
protected static $name = 'This is a Sedan';
}
Sedan::what_vehicle();
程序输出:
Sedan
This is a Vehicle
static示例:
<?php
class Vehicle {
protected static $name = 'This is a Vehicle';
public static function what_vehicle() {
echo get_called_class()."\n";
echo static::$name;
}
}
class Sedan extends Vehicle {
protected static $name = 'This is a Sedan';
}
Sedan::what_vehicle();
程序输出:
Sedan
This is a Sedan
总结
看看上一篇文章,已经一个多月没更新过博客了,忙是一部分,主要的还是自己懈怠了,以后还得坚持。这篇文章写的也有点没感觉。