网络编程
位置:首页>> 网络编程>> php编程>> php中-> 、=>、::、$this->四种常见符号使用方法技巧

php中-> 、=>、::、$this->四种常见符号使用方法技巧

  发布时间:2023-05-30 07:46:24 

标签:PHP,=>,->,::,$this

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->表示实例化后调用具体对象

 我们一般在一个类的内部使用本类的属性或方法时,就使用$this->

<?php
Class a{
   Var $name;
   Function Getname(){
       Echo $this->name;
   }
}
$name1 = new a;
$name1->name = '赋值给name1';
$name1->Getname();
//输出结果为 赋值给name1
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com