概述
正在学习设计模式,之前有一篇文章关于单例模式的文章,重新读了这篇文章,发现对static关键字掌握不是很牢靠,重新温习一下。
static关键字
PHP手册里对static关键字的介绍如下:
复制代码 代码如下:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as
本文介绍了PHP中new self()和new static()的区别,分享给大家,也给自己留个笔记。
1.new static()是在PHP5.3版本中引入的新特性。
2.无论是new static()还是new self(),都是new了一个新的对象。
3.这两个方法new出来的对象有什么区别呢,说白了就是new出来的到底是同一个类实例还是不同的类实例呢?
为了探究上面的问题,我们先上一段简单的代码:
class Father {
public function getNewFathe
本文实例讲述了php面向对象程序设计中self与static的区别。分享给大家供大家参考,具体如下:
1、假设我们有个Car类,它有2个方法:model()和getModel()。
class Car{
public function model(){
//这里我们使用了关键字self
self::getModel();
}
protected function getModel(){
echo 'I am car';
}
}
实例化后调用方法:
$car = new
本文实例讲述了PHP中类属性与类静态变量的访问方法。分享给大家供大家参考,具体如下:
<?php
/* PHP类属性与类静态变量的访问
* Created on 2016-7-13
*/
class test
{
const constvar='hello world';
static $staticvar='hello world';
function getStaticvar(){
return self::$staticvar;
}
}
$obj=new test