简体   繁体   English

这个php符号叫什么?

[英]What is this php notation called?

I saw this somewhere on the Internet. 我在互联网上看到了这个地方。 Can't remember where but I tried it out and it works. 不记得在哪里,但我尝试了它,它的工作原理。 It's supposed to allow for sequential execution of functions. 它应该允许顺序执行功能。

Chmod('file', 0777).chroot('file','root').redirect('/') ;

By adding '.' 通过增加 '。' after my function calls I can immediately execute another function. 我的函数调用后,我可以立即执行另一个函数。 I've searched the entire Internet for it but the problem is I don't know what it is called so I decided to ask here. 我已经搜索了整个互联网,但问题是我不知道它叫什么,所以我决定在这里问。 I would like to read its full documentation. 我想阅读其完整的文档。

This is not a "notation", just a weird exploitation of the concatenation operator (the dot). 这不是一种“符号”,只是对连接运算符(点)的奇怪利用。

It basically exploits PHP's type-juggling , converting each of the function return values to a string. 它基本上利用了PHP的类型 - 杂乱 ,将每个函数返回值转换为字符串。

chmod() and chown() will return booleans, implicitly casted to empty strings. chmod()chown()将返回布尔值,隐式转换为空字符串。
redirect() is a function probably provided to you by a framework, and most likely returns void (ie noting), which is again casted to an empty string. redirect()是一个可能由框架提供给你的函数,很可能返回void(即注释),它再次被转换为空字符串。

It will not work with functions that return arrays, or objects that don't implement the __toString() magic method - the only value types that can't be casted to string . 它不适用于返回数组的函数,或者不实现__toString()魔术方法的对象 - 唯一不能转换为string值类型。

The following will make more sense: 以下将更有意义:

$value = Chmod('file', 0777).chroot('file','root').redirect('/') ;

... only you don't actually care about $value , which you know would be a useless empty string. ...只有你实际上并不关心$value ,你知道这将是一个无用的空字符串。

I assume you find it "nice" and given my explanation, will probably think it's also "smart", but it is technically wrong. 我认为你发现它“很好”并且根据我的解释,可能会认为它也“很聪明”,但它在技术上是错误的。

Replacing the dots with semi-colons would be doing it "the right way". 用分号代替点将是“正确的方式”。 It's just that we are used to reading dots as method chaining in other languages, and is thus easier to read them as opposed to the semi-colons. 只是我们习惯于在其他语言中读取点作为方法链,因此与半冒号相比更容易阅读它们。

So, don't be surprised when somebody else calls it "ugly" - you're not supposed to do it and it is technically a "hack". 因此,当其他人称之为“丑陋”时不要感到惊讶 - 你不应该这样做,而且从技术上讲它是“黑客”。

You cannot do that in PHP. 你不能用PHP做到这一点。 The dot . . operator means only one thing in the language, and that is concatenation of strings. 运算符只表示语言中的一个内容,即字符串的连接。

You can do a method chaining, which is similar. 你可以做一个方法链接,这是类似的。 That is achieved by returning $this from every not final (which means it should return actual result or make final action) method in the class. 这是通过从类中返回$this来实现的(这意味着它应该返回实际结果或做出最终操作)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM