Formal PHP semantics?

I have been instructed to learn PHP, but there are many things that I do not understand. For example, the concept of "variable functions" is not the one I saw elsewhere. There are many other examples, but for brevity, I found PHPWTF , which has many examples of PHP features.

Most of the other languages ​​that I used have either an official specification (e.g. Haskell 2010 ), or at least a research paper on their formal semantics (e.g. this is for Javascript ). However, I cannot find anything comparable for PHP.

There is an official "language link". However, it is very informal, reads like a wiki and skips entire sections (for example, a syntax section does not define syntax at all). Confirming what I suspected, this guy tells me that there is no official specification or even undefined syntax.

Wikipedia has an article on “PHP Syntax and Semantics,” but it only touches on syntax and barely mentions semantics.

One document I found in PHP is this document on destination semantics . This is a very small piece of language and probably not very useful to me without any context. There is also this document in SaferPHP , which supposedly should work with some PHP definition, although I could not see it.

Interpreters / compilers provide semantics, so I decided to look at them. However, the Zend source is intimidating (although it provides a useful test case ) and HipHop runs up to 2.7 million LoC . (I find it amazing that people put a lot of effort into writing compilers for the language, they didn't even write something like specification.)

I thought about looking at the type systems for PHP for guidance, as TypeScript contains some guidelines for JavaScript. I found these tantalizing slides in Hack , an optional type system for PHP. However, it is just slides, and the project seems to be internal to Facebook at the moment.

Does anyone know anything better than these semantics of the poor? Or is everyone just "learning by example"?

+6
source share
5 answers

This answer comes a little after your initial question, but now we finally have formal semantics for PHP. Check it out: http://www.phpsemantics.org . A document on this was recently published in ECOOP 2014, if you are interested, you can find the link on the web page that I linked. Best wishes.

+2
source

It seems that you are not following an official standard (which may be useful, for example, for someone who writes an independent consistent implementation), but also to present a language that will allow you to make a consistent understanding of it. Unfortunately, this cannot be, because PHP does not have a consistent formal model. It grew organically and is now saddled with inconsistencies, best known in the name of functions and methods, but also in details like what is considered true and false , and other similar disturbing details.

The best thing that can be done to get closer to PHP, in my opinion, is a good perception of the basic functions and libraries for the "received" ones that you need to follow, and (to read existing code without distraction) for anti-templates that are too common in real PHP scripts. I assume that it is best to learn PHP under the guidance of people who can work effectively with it, but I did not have that luxury. (As for the documentation: It took me forever before I noticed that you can use square brackets to index into strings. The function may be mentioned somewhere in the documentation, but not then, at least not wherever it belongs. )

This article gives a nice overview of what makes the semantic model that you want impossible. (You might want to skip the introductory rant and go straight to discussing PHP functions. There are many, many other similar texts. Quote: "PHP was originally designed specifically for non-programmers (and, between the lines, non-programs), he could not escape your roots. "

Do not get me wrong: I work with PHP, and although this is not my favorite language, I would not say that I hate it. I would say that to work effectively with him you need to be aware of his nature and limitations. If you approach this from Haskell, you are in shock.

+3
source

Does not refer directly to your request, but explains some magic PHP variables.

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

+1
source

Interest Ask. I would consider the manual as an official reference to the language; I appreciate that this is not exactly a “formal link” in the sense in which you are looking, but I don’t know how much such a thing would be as desirable as something you can learn.

I am not familiar with PHPWTF, but I would suggest that it is in the same form as the Fractal Of Bad Design blog post (previously associated with @alexis). I cannot look into the heads of any authors, but it seems to me that they are written in terms of the fact that PHP is not needed. Religious wars often dominate the Internet and in programming; The browser you prefer, the IDE / editor you use, your operating system, and your choice of structure all have the same cruel, guerrilla, and unyielding treatment. Programming languages, unfortunately, are no different.

Of course, it is true that PHP has a number of inconsistencies in the design, in particular, on how zeros are processed, and in the ordering of parameters in standard functions. However, it is also true that PHP was extremely successful, despite all this. For a long time he was depressed in reliability at 5.0 and 5.1, 5.2 was stable, but perhaps not an entrepreneur, and he finally reached the age of 5.3 onwards.

Although this may be due to my biases, I feel a consensus among the users that I read on Stack Overflow that all popular languages ​​have their place. This is partly an answer to the fact that those we don’t love will not disappear, and partly it is possible that learning .net, Java, Perl, Ruby, PHP, Python, etc. - it's always good. Perhaps we are also collectively tired of flaming wars over everyone (Java is bloated, PHP is incompatible, Microsoft is a vendor lock, Rails is unstable, etc.).

I am off topic, but I tend to consider this particular point of view as worthy of attention, especially for those who traditionally would like to disagree with it regarding PHP.

To solve the purpose of your question, how should you study? Well, learning by example is a great approach - you just need to find out what examples you need to learn. Finding a "PHP tutorial" and a "PHP beginner" will work; perhaps, as is the case with any language; offer a mixture of excellent and terrible material. It can be argued that low barriers to entry into PHP created a large supply of unsafe and poorly written "how to" articles, and of course I saw a lot!

I think the solution is to look directly at the code from well-designed projects and study there. For instance:

  • Symfony2 (and components)
  • Zend framework
  • eat
  • Propel
  • Doctrine

Ah, almost forgot; this site is also a good place to start.


Script message: they may be referenced by a different name, but I expect all of them to have function variables. For example, in JavaScript, this is object[myFunc](); where myFunc is the string.

+1
source

This is not entirely formal semantics, but after all these years, the HHVM project has produced the PHP specification !

0
source

All Articles