在laravel中,self关键字用于替代类名,可以引用当前类的静态成员变量和静态函数,也用于抑制多态行为,可以引用当前类的函数而非子类中覆盖的实现,self总是指向当前类以及类实例。
本文操作环境:Windows10系统、Laravel6版、Dell G3电脑。
laravel中self的用法是什么
静态成员函数内不能用 this
调用非成员函数,但可以用 self
调用静态成员函数/变量/常量;其他成员函数可以用 self
调用静态成员函数以及非静态成员函数。随着讨论的深入,发现 self
并没有那么简单。鉴于此,本文先对几个关键字做对比和区分,再总结 self
的用法。
与 parent
、 static
以及 this
的区别
要想将彻底搞懂 self
,要与 parent
、 static
以及 this
区分开。以下分别做对比。
parent
self
与 parent
的区分比较容易: parent
引用父类/基类被隐盖的方法(或变量), self
则引用自身方法(或变量)。例如构造函数中调用父类构造函数:
class Base { public function __construct() { echo "Base contructor!", PHP_EOL; } } class Child { public function __construct() { parent::__construct(); echo "Child contructor!", PHP_EOL; } } new Child; // 输出: // Base contructor! // Child contructor!
static
static
常规用途是修饰函数或变量使其成为类函数和类变量,也可以修饰函数内变量延长其生命周期至整个应用程序的生命周期。但是其与 self
关联上是PHP 5.3以来引入的新用途:静态延迟绑定。
有了 static
的静态延迟绑定功能,可以在运行时动态确定归属的类。例如:
class Base { public function __construct() { echo "Base constructor!", PHP_EOL; } public static function getSelf() { return new self(); } public static function getInstance() { return new static(); } public function selfFoo() { return self::foo(); } public function staticFoo() { return static::foo(); } public function thisFoo() { return $this->foo(); } public function foo() { echo "Base Foo!", PHP_EOL; } } class Child extends Base { public function __construct() { echo "Child constructor!", PHP_EOL; } public function foo() { echo "Child Foo!", PHP_EOL; } } $base = Child::getSelf(); $child = Child::getInstance(); $child->selfFoo(); $child->staticFoo(); $child->thisFoo();
程序输出结果如下:
Base constructor! Child constructor! Base Foo! Child Foo! Child Foo!
在函数引用上, self
与 static
的区别是:对于静态成员函数, self
指向代码当前类, static
指向调用类;对于非静态成员函数, self
抑制多态,指向当前类的成员函数, static
等同于 this
,动态指向调用类的函数。
parent
、 self
、 static
三个关键字联合在一起看挺有意思,分别指向父类、当前类、子类,有点“过去、现在、未来”的味道。
this
self
与 this
是被讨论最多,也是最容易引起误用的组合。两者的主要区别如下:
-
this
不能用在静态成员函数中,self
可以; - 对静态成员函数/变量的访问, 建议 用
self
,不要用$this::
或$this->
的形式; - 对非静态成员变量的访问,不能用
self
,只能用this
; -
this
要在对象已经实例化的情况下使用,self
没有此限制; - 在非静态成员函数内使用,
self
抑制多态行为,引用当前类的函数;而this
引用调用类的重写(override)函数(如果有的话)。
self
的用途
看完与上述三个关键字的区别, self
的用途是不是呼之即出?一句话总结,那就是: self
总是指向“当前类(及类实例)”。详细说则是:
- 替代类名,引用当前类的静态成员变量和静态函数;
- 抑制多态行为,引用当前类的函数而非子类中覆盖的实现;
槽点
- 这几个关键字中,只有
this
要加$
符号且必须加,强迫症表示很难受; - 静态成员函数中不能通过
$this->
调用非静态成员函数,但是可以通过self::
调用,且在调用函数中未使用$this->
的情况下还能顺畅运行。此行为貌似在不同PHP版本中表现不同,在当前的7.3中ok; - 在静态函数和非静态函数中输出
self
,猜猜结果是什么?都是string(4) "self"
,迷之输出; -
return $this instanceof static::class;
会有语法错误,但是以下两种写法就正常:
$class = static::class;
return $this instanceof $class;
// 或者这样:
return $this instanceof static;
【相关推荐:laravel视频教程】
以上是laravel中self的用法是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

taskManagementtoolSareessential forefectiverMototeprojectManagementbyPrioritizingTaskSandTrackingProgress.1)usetoolsliketrelliketreloandasanatosetprioritieswithlabelsortags.2)

1)itoptimizeseLizeSeloQuentModelloAdingWithlazyProxies.3)

最佳的全栈Laravel应用部署策略包括:1.零停机部署,2.蓝绿部署,3.持续部署,4.金丝雀发布。1.零停机部署使用Envoy或Deployer自动化部署过程,确保应用在更新时保持可用。2.蓝绿部署通过维护两个环境实现无停机部署,并允许快速回滚。3.持续部署通过GitHubActions或GitLabCI/CD自动化整个部署流程。4.金丝雀发布通过Nginx配置,将新版本逐步推广给用户,确保性能优化和快速回滚。

toscalealaravelApplication有效,焦点databaseSharding,缓存,负载平衡和microservices.1)实现DataBaseShardingTodistAcribedateAtaCrossmultipledataBasesForimProvesforimpRevemperformance.2)uselaravel'scachingsystemystemystemystemystemnememmemememememcachedtebachedtorcachedtobcachebab

doovercomecommunicationbarriersIndistributedTeams,使用:1)VideoCallSforface-face-Faceinteraction,2)setClearresponsEtimepections,3)chooseappropropraproproprapropropriatecommunicationTools,4)CreatseateAteAteAteamCommunicationGuide和5)建立PemersonalboundariestariestopreventBreventBurniationBurnication.the

laravelbladeenhancesfrontendtemplatinginflatinginflationll-stackprojectsbyferingCleanSyntaxandaxandpoperfelfulfeatures.1)itallowsforeasyvariableasyvariabledisplayandControlstructures.2)bladesuportsuportsuportscreatingingingingingingingingingingangingandredreingscomponents components components components,aidinginmanagingcomplexuis.3)

laravelisidealforll-stackapplicationsduetoitselegantsyntax,complastissionecosystem和perperatefulfeatures.1)UseeloquentormForintuiveDiendbackendDatamanipulation,butavoidn 1Queryissues.2)

forremotework,iusezoomforvideOcalls,Slackformessing,trelloforprojectmanagement,and gitgithubForCodeCollaboration.1)Zoomisreliable forlailible forlargemeetingsbuthastimelimitsonthefreeversion.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。