php中this,self,parent,const关键字的用法
this,self,parent三个关键字之间的区别,从字面上比较好理解,分别是指这、自己、父亲。
我们先建立几个概念,这三个关键字分别是用在什么地方呢?
我们初步解释一下,this是指向当前对象的指针,self是指向当前类的指针,parent是指向父类的指针。
< ?php
// 测试this
class test_this{
// 定义变量
private $content;
// 定义构造函数
public function __construct($content){
$this->content= $content;
}
// 定义析构函数
public function __destruct(){}
//定义打印函数
public function printThis(){
echo $this->content.'
';
}
}
// 实例化对象
$test=new test_this('我测试this用法');
$test->printThis();
// 再一次实例化对象
$test=new test_this('我再一次测试this用法');
$test->printThis();
我测试this用法
我再一次测试this用法
< ?php
// 测试self
class test_self{
// 定义静态变量
private static $first_count;
private $last_count;
//直接用self调用变量的值赋值给另一个变量
function __construct(){
$this->last_count = ++ self::$first_count;
}
function __destruct(){}
function printSelf(){
echo $this->last_count.'
';
}
}
//实例化对象
$abc=new test_self();
$abc->printSelf();
$abc=new test_self();
$abc->printSelf();
1
2
< ?php
// 基类
class test_parent{
// 定义姓名
public $name;
function __construct($name){
$this->name=$name;
}
}
//派生类 继承test_parent
class test_son extends test_parent{
// 定义性别
public $gender;
// 定义年龄
public $age;
// 继承类的构造函数
function __construct($gender,$age){
// 使用parent调用父类的构造函数,来进行对父类的实例化
parent::__construct('明凯');
$this->gender=$gender;
$this->age=$age;
}
function __destruct(){}
function printParent(){
echo $this->name.'是个'.$this->gender.',今年'.$this->age.'岁'.'
';
}
}
// 实例化test_son对象
$nostop=new test_son('女性','22');
// 执行输出函数
$nostop->printParent();
明凯是个女性,今年22岁
$this
$this表示当前实例,在类的内部方法访问未声明为const及static的属性时,使用$this->value=’phpernote’;的形式。常见用法如:
$this->属性
$this->方法
在类里面调用当前类的属性和方法有三种方法,分别是self、parent、$this,这三个关键字的区别是:self用来指向当前的类;parent用于指向当前类的父类,可以使用该关键字调用父类的属性和方法;$this用来在类体内调用自身的属性和方法。
static
关键字可以是self(在类内部调用静态成员时所使用)静态成员所在的类名(在类外调用类内部的静态成员时所使用)
声明一个静态变量如下:
static $val='';
只存在于函数作用域的变量,函数执行之后变量的值不会丢失,只会初始化一次,初始化静态变量不能使用表达式,不用全局变量代替是因为全局变量会被所有函数访问容易造成维护不宜。
在类中使用static有两种主要用途、定义静态成员和定义静态方法。静态成员只保留一个变量的值,这个值对所有实例都是有效的,如下:
< ?php
class Book{
static $num=0;
public function showMe(){
echo"您是第".self::$num."位访客"."
";
self::$num++;
}
}
$book1=new Book();
$book1->showMe();
$book2=new Book();
$book2->showMe();
$book3=new Book();
$book3->showMe();
$book4=new Book();
$book4->showMe();
echo"您是滴".Book::$num."位访客";
?>
您是第0位访客
您是第1位访客
您是第2位访客
您是第3位访客
您是滴4位访客
另外需要注意的是如果类的方法是static的,他所访问的属性也必须是static的。
final
最终的类和方法,不能继承,该关键字修饰的方法不能被重写。一般用法如下:
< ?php
final class MyClass{//此类将不允许被继承
final function fun1(){......}//此方法将不允许被重写
}
const
在类的内部方法访问已经声明为const及static的属性时,需要使用self::$name的形式调用。
< ?php
class clss_a{
private static $name="static class_a";
const PI=3.14;
public $value;
public static function getName(){
return self::$name;
}
//这种写法有误,静态方法不能访问非静态属性
public static function getName2(){
return self::$value;
}
public function getPI(){
return self::PI;
}
}
注意const属性的申明格式是const PI=3.14,而不是const $PI=3.14。
self
self表示类本身,指向当前的类。通常用来访问类的静态成员、方法和常量。
Laravel 5.4 支持多个邮箱和Mail Driver并能够任意切换队列发送的方法 PHP面对对象设计模式之单例模式
Ahaa, its pleasant discussion regarding this post at this place at this weblog, I have read all that, so now me also commenting at this place.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
I want looking through and I conceive this website got some genuinely useful stuff on it!