php中-> 、=>、::、$this->四种符号在代码中很常见,使用很广泛。这篇文章主要介绍了php中-> 、=>、::、$this->四种常见符号使用方法技巧。
可以把->理解成调用的意思
<?php
Class a{
Var $id;
Function add(){
$this->id="test";
echo "abc";
}
}
$b = new a;
$b->add(); //调用类a中的add()方法,输出为abc
Echo $b->id; //调用类a中的属性id,输出为test
?>
<?php $arr1 =array(0=>"php",1=>"is",the=>"the"); Echo $arra[0],$arr1[1],$arr["the"]; //对应输出设置的值
正常的情况我们用实例化方法来调用类中的属性或方法,但使用::可以不需要实例化对象,直接调用即可。
比如:
<?php
Class b{
Var $name="test";
Function Getname(){
Echo "test is good";
}
}
//直接调用:
Echo b::Getname();//输出为test isgood
我们一般在一个类的内部使用本类的属性或方法时,就使用$this->
<?php
Class a{
Var $name;
Function Getname(){
Echo $this->name;
}
}
$name1 = new a;
$name1->name = "赋值给name1";
$name1->Getname();
//输出结果为 赋值给name1
到此这篇关于php中-> 、=>、::、$this->四种常见符号使用方法技巧的文章就介绍到这了,更多相关php中-> 、=>、::、$this->符号内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!