分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

PHP static 和 self的区别

发布时间:2023-09-06 02:00责任编辑:胡小海关键词:PHP

php是世界上最好的语言

php从面向过程走到现在成熟的面向对象体系, 在php面向对象中,静态变量的调用我们可以用这两个self::method和 static::method, 但是很多童鞋迷惑,不理解self::method和static::method有什么区别,下面给出两个例子一看究竟:

例子1:

 1 class Car { 2 ????public static function model() 3 ????{ 4 ????????self::getModel(); 5 ????} 6 ????protected static function getModel() 7 ????{ 8 ????????echo "This is a car model"; 9 ????}10 }11 Car::model(); //This is a car model12 echo ‘<br />‘;13 Class Taxi extends Car {14 ????protected static function getModel()15 ????{16 ????????echo "This is a Taxi model";17 ????}18 }19 Taxi::model(); //This is a car model

总结: self::getModel()调用方法getModel(), 子类的方法getModel()实际意义上没有重载父类的方法getModel().

例子2:

 1 class Car { 2 ????public static function model() 3 ????{ 4 ????????static::getModel(); 5 ????} 6 ????protected static function getModel() 7 ????{ 8 ????????echo "This is a car model"; 9 ????}10 }11 Car::model(); ?//This is a car model12 echo ‘<br />‘;13 Class Taxi extends Car {14 ????protected static function getModel()15 ????{16 ????????echo "This is a Taxi model";17 ????}18 }19 Taxi::model(); //This is a Taxi model

总结: self::getModel()调用方法getModel(), 子类的方法getModel()重载了父类的方法getModel().

童鞋们,理解了吗?

如有疑惑,欢迎评论

PHP static 和 self的区别

原文地址:https://www.cnblogs.com/hellow-world/p/9188607.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved