php怎么调用static 方法
2022-12-02 09:01:16
192
{{single.collect_count}}

php调用static方法:1、创建一个PHP示例文件;2、定义“class Char{...}”;3、创建“public static functionJoin(){...}”;4、使用“Char::Join();$test->Join();$test::Join();”方式调用即可。

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。

php怎么调用static 方法?

php中使用static方法

代码如下:

<?phpclass Char{public static $number = 0;public static $name;function __construct($what){self::$name = $what;}public static functionJoin(){self::$number++;echo self::$number," Is :",self::$name,"<br />";}}$test = new Char('a');Char::Join();#注意, 在静态方法中不能访问非静态变量#Char::Join();#$test->Join();#$test::Join();可以这样调用
登录后复制

相关介绍:

静态属性

1: static静态属性单独存在类中(属于类),不属于对象。因此只要类声明完毕,该属性就存在。既访问该静态属性不需要依赖于对象就可以访问

2:static 在类中一直有个,因此他被所有对象共享,一人影响,其他共享。

静态方法

普通方法存放在类种,在内存中只有1份。静态方法也如此。 区别 :普通方法需要对象去调用,需绑t h i s 。 静 态 方 法 不 需 要 绑 定 this。 静态方法不需要绑定this。静态方法不需要绑定this,则通过类名即可调用

推荐学习:《PHP视频教程

以上就是php怎么调用static 方法的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}