php中-> 、=>、::、$this->四种常见符号使用方法技巧
时间:2023-05-30 07:46:24
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
标签:PHP,=>,->,::,$this
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现读取并显示图片的两种方法
2023-12-20 12:51:14
解读Opencv中Filter2D函数的补全方式
2022-06-16 23:11:15
![](https://img.aspxhome.com/file/2023/5/98885_0s.png)
c#将Excel数据导入到数据库的实现代码
2024-01-25 21:53:43
![](https://img.aspxhome.com/file/2023/2/68632_0s.jpg)
[翻译]标记语言和样式手册 Chapter 12 CSS布局
2008-02-13 14:19:00
![](https://img.aspxhome.com/file/UploadPic/20082/13/2008213144620374s.jpg)
Python获取当前公网ip并自动断开宽带连接实例代码
2021-08-28 12:40:27
PHP图片库imagemagick安装方法
2023-11-16 18:54:38
python转化excel数字日期为标准日期操作
2021-01-14 22:38:59
![](https://img.aspxhome.com/file/2023/7/107877_0s.jpg)
在Django中进行用户注册和邮箱验证的方法
2022-02-01 16:18:35
漫谈前端开发中的团队合作
2009-02-05 21:02:00
python pycharm中使用opencv时没有代码自动补全提示的解决方案
2022-05-10 14:34:15
![](https://img.aspxhome.com/file/2023/2/68342_0s.jpg)
Mac中安装nvm的教程分享
2024-05-03 15:36:55
python安装cx_Oracle和wxPython的方法
2022-07-31 11:33:04
![](https://img.aspxhome.com/file/2023/1/93451_0s.png)
MySQL实时监控工具orztop的使用介绍
2024-01-13 18:15:17
![](https://img.aspxhome.com/file/2023/9/66619_0s.png)
Sublime Text v4.0(4143)安装方法
2023-09-27 12:04:50
![](https://img.aspxhome.com/file/2023/4/68914_0s.jpg)
Go语言自定义linter静态检查工具
2024-02-13 15:32:20
![](https://img.aspxhome.com/file/2023/7/113077_0s.png)
python简单获取数组元素个数的方法
2023-03-08 15:39:29
Python虚拟环境venv用法详解
2023-10-26 12:37:12
![](https://img.aspxhome.com/file/2023/7/121057_0s.png)
ADO.NET数据库访问技术
2024-01-13 03:17:27
![](https://img.aspxhome.com/file/2023/0/81000_0s.png)
[翻译]标记语言和样式手册 Chapter 1 清单
2008-01-15 10:26:00
![](https://img.aspxhome.com/file/UploadPic/up/2008011510563788.gif)
MySQL中count(*)、count(1)和count(col)的区别汇总
2024-01-23 07:13:43