PHP形式语义?

I am tasked with learning PHP, but there are many things I don't understand. For example, the concept of "variable functions" is not one I've seen anywhere else. There are many other examples, but for brevity, I found PHPWTF, which has many examples of PHP's idiosyncrasies.

Most other languages I've used have either a formal specification (e.g., Haskell 2010) or at least a research paper on their formal semantics (e.g., this for Javascript). However, I can't find anything comparable for PHP.

There is an official "language reference". However, it is very informal, reads like a wiki, and is missing entire sections (e.g., the section on syntax doesn't define the syntax at all). Confirming what I suspected, this guy tells me that there is no official specification, nor even a defined syntax.

Wikipedia has an article on "PHP syntax and semantics", but it only touches on the syntax, and barely mentions semantics.

One paper I've found on PHP is this paper on its assignment semantics. This is a very small fragment of the language and probably not much use to me without some context. There is also this paper on 'SaferPHP', which presumably has to work with some definition of PHP, though I couldn't see any.

Interpreters/compilers provide a semantics, so I thought to look at these. However, the Zend source is intimidating (though it does provide useful test cases), and HipHop runs to 2.7 million LoC. (I find it amazing that people have poured enormous effort into writing compilers for a language without ever writing something like a specification.)

I thought of looking at type systems for PHP for guidance, much like TypeScript provides some guidance for JavaScript. I found these tantalising slides on Hack, an optional type system for PHP. However, it's just slides, and the project seems to be an internal one at Facebook at this time.

Does anyone know of anything better than these poor man's semantics? Or does everyone just "learn by example"?

php
dsfdsfsdf45489
dsfdsfsdf45489 PHP最好由其创建者RasmusLerdorf总结:“我完全不知道如何编写编程语言,我只是在路上添加了下一个逻辑步骤。”
接近 7 年之前 回复
doushenmao9036
doushenmao9036 是的,似乎就是这样。虽然我认为Javascript也是如此,并且它已经适合某种形式化。也许PHP也可以。
接近 7 年之前 回复
du512926
du512926 “形式语义学”?别笑我。正如蒂姆所说,它的定义是它的作用。它的作用是“有机地”增长(一种委婉的“ooh,让我们添加这个很酷的功能”,据我所知,这主要是受到其他语言嫉妒的推动)。
接近 7 年之前 回复
douguo6472
douguo6472 PHP的语义是由官方实现的行为定义的AFAIK。
接近 7 年之前 回复

5个回答



这个答案在您的初始问题之后有点说,但现在我们终于有了PHP的形式语义。 请查看: http://www.phpsemantics.org 。 有关它的论文最近已在ECOOP 2014会议论文集中发表,如果您有兴趣,可以在我链接的网页中找到该链接。 问候。</ p>
</ div>

展开原文

原文

This answer comes a bit after your initial question, but now we finally have a formal semantics for PHP. Check it out: http://www.phpsemantics.org. A paper about it has been recently published in the ECOOP 2014 proceedings, if you are interested you can find the link in the webpage I linked. Regards.



您似乎没有遵循官方标准(例如,对于编写独立合规实施的人可能有用) ,但是对于能够让你对它有一致意义的语言的介绍。 不幸的是,不可能有这样的事情,因为 PHP背后没有一个连贯的正式模型。</ strong>它有机地发展,现在背负着不一致,最臭名昭着的功能和方法命名,但也有一些细节 就像什么算作 true </ code>和 false </ code>,以及其他类似令人担忧的细节。</ p>

最好的人可以做到接近PHP,在我的 意见,是为了获得核心功能和库的良好感觉,对于你需要注意的“问题”,以及(为了阅读现有的代码而不会分散注意力)的反模式,这些模式在 真实的PHP脚本。 我的猜测是,最好在知道如何有效地使用它的人的指导下学习PHP,但我没有那么奢侈。 (关于文档:在我注意到你可以使用方括号来索引字符串之前我花了很长时间。这个特性可能会在文档中的某个地方提到,但至少在它所属的任何地方都可以提到。)</ p >

这篇文章给出了一个很好的参观方式,可以创建出你想要的语义模型。 (你可能想跳过开场咆哮,直接去讨论PHP功能。)还有许多其他类似的文本。 引用:“PHP最初是为非程序员明确设计的(并且,在行之间进行读取,非程序);它还没有很好地逃脱它的根源。”</ em> </ p>

< 请不要误会我的意思:我使用PHP,虽然这不是我最喜欢的语言,但我不会说我讨厌它。 我会</ em>说要有效地使用它,必须意识到它的性质和局限性。 如果你是从Haskell来到这里的话,你会非常震惊。</ p>
</ div>

展开原文

原文

It seems that you're not after an official standard (which might be useful, for example, to someone writing an independent conforming implementation), but for a presentation of the language that will allow you to make coherent sense of it. Unfortunately there cannot be such a thing, because PHP does not have a coherent formal model behind it. It has grown organically and is now saddled with inconsistencies, most notoriously in function and method naming but also in little details like what counts as true and false, and other similarly worrisome details.

The best one can do to approach PHP, in my opinion, is to get a good feel for the core features and libraries, for the "gotcha's" that you need to watch out for, and (in order to read existing code without distraction) for the anti-patterns that are all too common in real-world PHP scripts. My guess is that it's best to learn PHP under the tutelage of people who know how to work with it effectively, but I didn't have that luxury. (Regarding the documentation: It took me forever before I noticed that you can use square brackets to index into strings. The feature may be mentioned somewhere in the documentation, but not, back then at least, anyplace where it belongs.)

This article gives a nice tour of the kind of things that make a semantic model of the kind you want impossible. (You may want to skip the opening rant and go straight to the discussion of PHP features.) There are many, many other similar texts. Quote: "PHP was originally designed explicitly for non-programmers (and, reading between the lines, non-programs); it has not well escaped its roots."

Don't get me wrong: I work with PHP, and although it's not my favorite language, I wouldn't say I hate it. I would say that to work effectively with it, one must be aware of its nature and limitations. If you're coming to this from Haskell, you're in for quite a shock.

dpgua04022
dpgua04022 我没有指出没有文档,但是要记录的行为并不是连贯的,因为它们属于你可以“获得”的统一模型。 每个方面都不同。 范围以一种方式工作,命名空间另一种方式。 有很多概述,但任何文档都将是某种大型列表,而不是统一模型。 我可能错了,但我不相信我错过了这一点。
接近 7 年之前 回复
doubi3996
doubi3996 我正要提出与OP相同的问题。 (我有一些PHP背景,希望加深对语言的理解)。 我认为你的答案忽略了这一点。 是的,可能没有任何“正式”语义,但是某些关键事物仍然存在某种行为方式 - 认为像参数传递,迭代器,范围等等。尽管有相当多的PHP程序员可以做到 那些知识,我仍然相当肯定它是以某种方式记录的。
接近 7 年之前 回复
dongtan9253
dongtan9253 它似乎肯定有坏/复杂的部分。 我希望有类似“Featherweight PHP”的东西(比如“Featherweight Java”,或“PHP的本质”(比如“Javascript的本质”)。一些剥离复杂功能的东西,以便严格解释基本语义。 Javascript以类似于PHP的方式被讽刺,但事实证明可以将其剥离为最小的语言以进行严格的分析。
接近 7 年之前 回复



不直接解决您的问题,但解释了PHP变量背后的一些神奇之处。</ p>

http://webandphp.com/how-php-manages-variables </ p >
</ div>

展开原文

原文

Doesn't directly address your inquiry, but explains some of the magic behind PHP variables.

http://webandphp.com/how-php-manages-variables

dsyct08008
dsyct08008 现在,我已经接受了这个作为答案,因为它实际上提供了有用的信息,而其他答案只是驳回了这个问题(在可疑的基础上)。
接近 7 年之前 回复
dongliu6848
dongliu6848 没错,但这很有用,谢谢!
接近 7 年之前 回复

Interesting question. I'd regard the manual as the official language reference; I appreciate it isn't quite "formal reference" in the sense you are seeking, but I don't know how much such a thing would be widely desired as something to learn from.

I'm not familiar with PHPWTF, but I'd guess it is in the same mould as the blog post Fractal Of Bad Design (linked by @alexis earlier). I can't peer into the mind of either author, but it seems to me that they are written from the perspective of wanting PHP to be bad. Religious wars frequently dominate on the internet and in programming — the browser you prefer, the IDE/editor you use, your operating system and your choice of framework have all had the same ferocious, partisan and unyielding treatment. Programming languages are, sadly, no different.

It is certainly true that PHP does have a number of design inconsistencies, in particular about how nulls are treated, and in the ordering of parameters in standard functions. However, it is also true that PHP has been hugely successful, despite all that. It spent a long time in the reliability doldrums in 5.0 and 5.1, 5.2 was stable but arguably not enterprise, and it's finally coming of age in 5.3 onwards.

Whilst this might be my biases emerging, I sense a consensus amongst users I read on Stack Overflow that all of the popular languages have their place. This is partly a response to the reality that the ones we dislike won't go away, and partly perhaps that learning .net, Java, Perl, Ruby, PHP, Python etc is pretty much always a good thing. Maybe we have also collectively tired of the flame-wars over each (Java is bloated, PHP is inconsistent, Microsoft is vendor lock-in, Rails is unstable, and so forth).

I've veered rather off-topic, but I tend to regard this particular viewpoint as worth reading, especially for those who would be traditionally minded to disagree with it in relation to PHP.

To address the purpose of your question, how should you learn? Well, learning by example is an excellent approach - one just needs to know which examples to learn. Searching for "PHP tutorial" and "PHP beginner" will — perhaps as is the case with any language — offer a mix of excellent and dreadful material. One might argue that PHP's low barriers to entry have given rise to a large stock of insecure and badly written "how to" articles, and I've certainly seen quite a few!

I think the solution is to look directly at code from well-engineered projects, and to learn from there. Such as:

  • Symfony2 (and Components)
  • Zend Framework
  • Guzzle
  • Propel
  • Doctrine

Ah, nearly forgot; this website is also a good place to start.


Post Script: they may be referred to by a different name in other languages, but I expect they all have variable functions. In JavaScript for example, it's object[myFunc]();, where myFunc is a string.

doushang9172
doushang9172 很高兴听你这样说! 我的观点将不再适用于修复大量此类怪癖的任何PHP版本(并且有太多或者有太多)。
接近 7 年之前 回复
duancao1951
duancao1951 too long
接近 7 年之前 回复
dongsuikai8286
dongsuikai8286 我无法想象从BNF规范中学习,但文本描述怎么说“这是一个表达式,这是一个声明,这是一个左值,这是一个参考,这是你得到的结合它们”? 例如,python文档可以很好地完成它。 但是,合理的正式模型可以捕获这样一个事实:在PHP中,func_get_args()不能用作函数参数?
接近 7 年之前 回复
dtmooir3395
dtmooir3395 谢谢你的想法。 我同意正式的定义是问题的一部分,但是“每个人都只是”通过实例“学习”吗? 无论是对还是错,我都将其解释为“什么是学习的好方法”。 FWIW,我认为正式/ BNF规格很难学习,但随后它可能因人而异。
接近 7 年之前 回复
dongyinju5977
dongyinju5977 +1链接到phptherightway,它看起来像一个很好的资源! 也就是说,OP并没有要求提供良好的文档,而是要求PHP的“正式语义”。 Phptherightway强调了PHP的最佳功能(不是最糟糕的,就像我提供的链接一样),但我没有看到任何可以作为该语言的正式模型的资格。
接近 7 年之前 回复



它不完全是形式</ em>语义,但是,经过这么多年,HHVM项目产生了 PHP规范!</ p>
</ div>

展开原文

原文

It's not exactly a formal semantics, but, after all these years, the HHVM project has produced a PHP specification!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问