php面向对象类中的$this,static,final,const,self及双冒号 :: 这几个关键字使用方法。
php中this,self,parent三个关键字的作用 this,self,parent三个关键字之间的区别,从字面上比较好理解,分别是指这、自己、父亲。我们先建立几个概念,这三个关键字分别是用在什么 地方呢?我们初步解释一下,this是指向当前对象的指针(姑且用c里面的指针来看吧),self是指向当前类的指针,parent是指向父类的指针。我 们这里频繁使用指针来描述,可能是因为没有更好的语言来表达。
content= $content; } function __destruct(){}//定义析构函数 function printcontent(){//定义打印函数 echo $this->content.'
'; }}$test=new test_this('北京欢迎你!'); //实例化对象$test->printcontent();//北京欢迎你!$test=new test_this('新年新气象!');//再一次实例化对象$test->printcontent();//新年新气象!echo '
';//self是指向类的本身,只跟类有关,跟任何对象实例无关class test_self{ private static $first_count; //定义静态变量 private $last_count; function __construct(){ $this->last_count=++self::$first_count;//直接用self调用变量的值赋值给另一个变量 } function __destruct(){} function print_self(){ print($this->last_count); }}$abc=new test_self();//实例化对象$abc->print_self();//1echo '
';//parent是指向父类的指针class test_parent{ //基类 public $name; //定义姓名 父类成员需要定义为public,才能够在继承类中直接使用 this来调用。 function __construct($name){ $this->name=$name; }}class test_son extends test_parent{ //派生类 继承test_parent public $gender;//定义性别 public $age; //定义年龄 function __construct($gender,$age){ //继承类的构造函数 parent::__construct('nostop');//使用parent调用父类的构造函数,来进行对父类的实例化 $this->gender=$gender; $this->age=$age; } function __destruct(){} function print_info(){ echo $this->name.'是个'.$this->gender.',今年'.$this->age.'岁'.'
'; }}$nostop=new test_son('女性','22');//实例化test_son对象$nostop->print_info();//执行输出函数 nostop是个女性,今年23岁?>
$this$this表示当前实例,在类的内部方法访问未声明为const及static的属性时,使用$this->value='phpernote';的形式。常见用法如:
$this->属性
$this->方法
举例如下:
查看代码打印
name=$name; } public function getname(){ return $this->name; } public function printname(){ echo $this->getname(); }}$myclass= new myclass(i like php);$myclass->printname();//输出:i like php?>
在类里面调用当前类的属性和方法有三种方法,分别是self、parent、$this,这三个关键字的区别是:self用来指向当前的类;parent用于指向当前类的父类,可以使用该关键字调用父类的属性和方法;$this用来在类体内调用自身的属性和方法。
static关键字可以是self(在类内部调用静态成员时所使用)静态成员所在的类名(在类外调用类内部的静态成员时所使用)
声明一个静态变量如下:
static $val='';
只存在于函数作用域的变量,函数执行之后变量的值不会丢失,只会初始化一次,初始化静态变量不能使用表达式,不用全局变量代替是因为全局变量会被所有函数访问容易造成维护不宜。
在类中使用static有两种主要用途、定义静态成员和定义静态方法。静态成员只保留一个变量的值,这个值对所有实例都是有效的,如下:
mymethod();$instance2=new myobject();$instance2->mymethod();//结果将分别打印2、4
showme();echo
;$book2=new book();$book2->showme();echo
;echo您是滴.book::$num.位访客;?>
结果将是:
您是滴0位访客
您是滴1位访客
您是滴2位访客
另外需要注意的是如果类的方法是static的,他所访问的属性也必须是static的。
final最终的类和方法,不能继承,该关键字修饰的方法不能被重写。一般用法如下:
。
另外,如果从类的内部访问const或者static变量或者方法,那么就必须使用自引用的self,反之如果从类的内部访问不为const或者static变量或者方法,那么就必须使用自引用的$this。
php双冒号::的用法双冒号操作符即作用域限定操作符scope resolution operator可以访问静态、const和类中重写的属性与方法。
在类定义外使用的话,使用类名调用。在php 5.3.0,可以使用变量代替类名。program list:用变量在类定义外部访问。
program list:在类定义外部使用::
程序运行结果:
fruit::showcolor()
apple::showcolor()
program list:使用作用域限定符
color; } } class banana { public $color; public function __construct() { $this->color = banana is yellow; } public function getcolor() { return apple::showcolor(); } } $banana = new banana; echo $banana->getcolor();?>
程序运行结果:banana is yellowprogram list:调用基类的方法
程序运行结果:show color