Guide to PHP 7.1

PHP 7.1 was an incremental feature release, offering new language constructs, significant performance improvements, and lower resource utilization, making it a natural choice for fast-paced business-critical applications.

In this article, we look at PHP 7.1 features, performance comparisons, end of life, and why the PHP 7.1 release marked such a big change for PHP.

PHP Internals News: Episode 59: Named Arguments – Derick Rethans

PHP Internals News: Episode 59: Named Arguments

In this episode of “PHP Internals News” I chat with Nikita Popov (Twitter, GitHub, Website) about his Named Parameter RFC.

The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode’s MP3 file, and it’s available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news

Transcript

Derick Rethans 0:18

Hi, I’m Derick, and this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 59. Today I’m talking with Nikita Popov about a few RFCs that he’s produced. Hello Nikita, how are you this morning?

Nikita Popov 0:35

Hey Derick, I’m great. How are you?

Derick Rethans 0:38

Not too bad, not too bad today. I think I made a decision to stop asking you to introduce yourself because we’ve done this so many times now. We have quite a few things to go through today. So let’s start with the bigger one, which is the named arguments RFC. We have in PHP eight already seen quite a few changes to how PHP deals with set up and things like that we have had an argument promotion in constructors, we have the mixed type, we have union types, and now named arguments, I suppose built on top of that, again, so what are named arguments?

Nikita Popov 1:07

Currently, if you’re calling a function or a method you have to pass the arguments in a certain order. So in the same order in which they were declared in the function, or method declaration. And what named arguments or parameters allows you to do is to instead specify the argument names, when doing the call. Just taking the first example from the RFC, we have the array_fill function, and the array_fill function accepts three arguments. So you can call like array_fill( 0, 100, 50 ). Now, like what what does that actually mean? This function signature is not really great because you can’t really tell what the meaning of this parameter is and, in which order you should be passing them. So with named parameters, the same call would be is something like: array_fill, where the start index is zero, the number is 100, and the value is 50. And that should immediately make this call, like much more understandable, because you know what the arguments mean. And this is really one of the main like motivations or benefits of having named parameters.

Derick Rethans 2:20

Of course developers that use an IDE already have this information available through an IDE. But of course named arguments will also start working for people that don’t have, or don’t want to use an IDE at that moment.

Nikita Popov 2:31

At least in PhpStorm, there is a feature where you can enable these argument labels for constants typically only. This would basically move this particular information into the language, but I should say that of course this is not the only advantage of having named parameters. So making code more self documenting is one aspect, but there are a couple couple more of them. I think one important one is that you can skip default values. So if you have a function that has many optional arguments, and you only want to say change the last one, then right now you actually have to pass all the arguments before the last one as well and you have to know: Well, what is the correct default value to pass there, even though you don’t really care about it.

Derick Rethans 3:19

If I remember correctly, there are a few functions in PHP’s standard library, where you cannot actually replicate the default value with specifying an argument value, because they have this really complex and weird kind

Truncated by Planet PHP, read more at the original (another 22256 bytes)

PHP 8.0.0 Alpha 1 available for testing – PHP: Hypertext Preprocessor

The PHP team is pleased to announce the first testing release of PHP 8.0.0, Alpha 1. This starts the PHP 8.0 release cycle, the rough outline of which is specified in the PHP Wiki. For source downloads of PHP 8.0.0 Alpha 1 please visit the download page.Please carefully test this version and report any issues found in the bug reporting system.Please DO NOT use this version in production, it is an early test version. For more information on the new features and other changes, you can read the NEWS file, or the UPGRADING file for a complete list of upgrading notes. These files can also be found in the release archive. The next release will be Alpha 2, planned for 9 Jul 2020.The signatures for the release can be found in the manifest or on the QA site.Thank you for helping us make PHP better.

Unit test naming conventions – Matthias Noback

Recently I received a question; if I could explain these four lines:

/** * @test */
public function it_works_with_a_standard_use_case_for_command_objects(): void

The author of the email had some great points.

  1. For each, my test I should write +3 new line of code instead write, public function testItWorksWithAStandardUseCaseForCommandObjects(): void
  2. PSR-12 say that “Method names MUST be declared in camelCase”. [The source of this is actually PSR-1].
  3. In PHPUnit documentation author say “The tests are public methods that are named test*” and left example below
  4. PHPStorm IDE from the last version gives for you ability to generate a TestCode and they do not use an underscore too:

    PHPUnit generating test code

  5. I opened a popular frameworks (Symfony, Laravel, YII2, CodeIgniter4) test folders in Github and then I opened a popular PHP library and also do not find underscore usage in test and @test annotation.

This made me realize that, yes, what I’m doing here is a convention, and maybe it’s not the most popular one.
Well, I’ve seen tests that use an even less conventional approach to method naming by using some kind of character that isn’t a space but still looks like one:

/** * @test */
public function it works with a standard use case for command objects(): void

This is too much for me, but who knows, in a couple of years maybe…

I’ll first respond to the objections now:

  1. I don’t think it’s bad to write 3 new lines for every test method.
    It’s not like it’s actual code that gets duplicated, it’s just an annotation.
    The code never has to be modified, so it will never suffer from Shotgun Surgery (unless you switch test frameworks maybe).
    Anyway, I have a live template configured in PHP so I only have to type it and it will complete this with /** @test */ public function etc. We’ll talk about it later.
  2. Philosophically speaking, I don’t think a test method is a regular object method.
    Yes, of course, it is technically a method. PHPUnit instantiates your TestCase and will call its methods.
    But that is just the way PHPUnit does it.
    You might as well write something like this:

    it('works with a standard use case for command objects');
    

    In fact, there are test frameworks that let you write tests like this.

    You will never find any normal code (a.k.a. production code) call these test methods.
    I do think that would look rather weird.

    Instead of regular methods, I think we should consider test methods to be documentation or more specific: specification.
    This makes other aspects than code style conventions more important: it needs to be readable and precise.
    And in my experience testItWorksWithAStandardUseCaseForCommandObjects is really hard to read.
    Then again, maybe I just have to practice.

    But what if your code style fixer automatically turns “snake_case” method names into camelCase ones?
    Just configure the tool to ignore your tests.

  3. The way things are done in framework documentation can, but doesn’t have to be considered the standard.
    The writer of the documentation has different goals than you have.
    Maybe they want to show the easiest way to get started with their framework.
    Maybe they just show the way that most users do it.
    Maybe nobody knows what most users do, and they just pick a way.
    What I do know is that the PHPUnit documentation also mentions this other way: “Alternatively, you can use the @test annotation in a method’s docblock to mark it as a test method.”
  4. Although I love PhpStorm, I’m totally not inclined to do as they do.
    As an example, they have invented the convention that we have to annotate all the exceptions that a method throws.
    They also have an inspection that warns you about calling a method that could throw an exception.
    I bet that a lot of developers have since been wrapping things in try/catch blocks and coming up with inventive ways of dealing with exceptions.
    Not because we have to, but because the IDE developers thought it would be a good idea.
    End of rant.
    Conclusion: don’t let the IDE determine how you write your code (unless you have thought about it and agree with it of course).
    When it comes to generating a test class and “generating test methods”: totally ignore what PhpStorm h

Truncated by Planet PHP, read more at the original (another 4445 bytes)

BigQuery: Calculate the MEDIAN in BigQuery – Pascal Landau

There is no MEDIAN() function in Google BigQuery, but we can still calculate the MEDIAN
with the
PERCENTILE_CONT(x, 0.5) or
PERCENTILE_DISC(x, 0.5)
functions. The difference between those two functions is the linear interpolation that is applied
when using PERCENTILE_CONT(x, 0.5) – so that’s probably what you want when dealing with numeric
values. Take the numbers 1,2,3,4 for example:

  • PERCENTILE_CONT(x, 0.5) yields 2.5 (as the 50% percentile is exactly between 2 and 3)
  • PERCENTILE_DISC(x, 0.5) yields 2 (as the 50% percentile is >= 2)

Example

SELECT PERCENTILE_CONT(x, 0.5) OVER() AS median_cont, PERCENTILE_DISC(x, 0.5) OVER() AS median_disc
FROM UNNEST([1,2,3,4]) as x
LIMIT 1

Result

median_cont median_disc
2.5 2

Caution: As of today (2020-06-20), BigQuery only supports PERCENTILE_CONT and PERCENTILE_DISC for window functions
(hence the OVER() clause and the LIMIT 1 in the example above):

PERCENTILE_CONT is under development, and we will publish the documentation once it is GA. We will support it as analytic function first, and we plan to support it as aggregate function (allowing GROUP BY) later.

Source: SO: percentile functions with GROUPBY in BigQuery

The more common use case is probably to calculate the median as a result of a GROUP BY statement.
I.e. I would like to write something like this to get the median of quantity per product_id.

SELECT product_id, PERCENTILE_CONT(quantity, 0.5) AS median
GROUP BY product_id

Right now, that is only possible for the average via
AVG()
but not for the median. But we can still work around that limitation by using the
PERCENTILE_CONT function on a window partitioned by product_id, then group by the product_id
(to get only one row per product_id)
and resolve a single median value via ANY_VALUE().

Working Example

<script src=”https://gist.github.com/paslandau/14940ec0fd34dc30b36377886c308ab3.js”><script src=”https://gist.github.com/paslandau/14940ec0fd34dc30b36377886c308ab3.js”>

Run on BigQuery

Open in BigQuery UI

BigQuery UI: MEDIAN in BigQuery example

Links

Notes

There is also the APPROX_QUANTILES()
function (mentioned here) that can by applied to a GROUP BY. I didn’t have a practical use case for approximate functions
yet, though. Thus, I don’t know the implications of “not using an exact calculation” and rather mention
this for the sake of completeness. Example:

SELECT product_id, APPROX_QUANTILES(quantity, 100)[OFFSET(50)] as approx_median
GROUP BY product_id

PHP Internals News: Episode 58: Non-Capturing Catches – Derick Rethans

PHP Internals News: Episode 58: Non-Capturing Catches

In this episode of “PHP Internals News” I chat with Max Semenik (GitHub) about the Non-Capturing Catches RFC that he’s worked on, and that’s been accepted for PHP 8, as well as about bundling, or not, of extensions.

The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode’s MP3 file, and it’s available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news

Transcript

Derick Rethans 0:18

Hi, I’m Derick, and this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 58. Today I’m talking with Max Semenik about an RFC that is proposed called non capturing catches. Hello Max, would you please introduce yourself.

Max Semenik 0:38

Hi Derick. I’m an open source developer, working mostly on MediaWiki. So that’s how I came to be interested in contributing to PHP.

Derick Rethans 0:50

Have you been working with MediaWiki for a long time?

Max Semenik 0:53

Something like 11 years, I guess.

Derick Rethans 0:56

That sounds like a long time to me. The RFC that you’ve made. What is the problem that is trying to address?

Max Semenik 1:03

In current PHP, you have to specify a variable for exceptions you catch, even if I you don’t need to use this variable in your code, and I’m proposing to change it to allow people to just specify an exception type.

Derick Rethans 1:20

At the moment, the way how you catch an exception is by using catch, opening parenthesis, exception class, variable, and you’re saying that you don’t have to do the name of the variable any more. I get that right?

Max Semenik 1:33

Yes.

Derick Rethans 1:34

Is that pretty much the only change that this is making?

Max Semenik 1:38

Yes, it’s a very small, and well defined RFC. I just wanted to do something small, as my start to contributing to PHP.

Derick Rethans 1:51

I’m reading the RFC, it states also that the what used to be an earlier RFC. How does that differ from the one that you’ve proposed?

Max Semenik 2:00

The previous RFC wanted to also permit a blanket catching of exceptions, as in anything. And that’s all, which, understandably, has caused some objections from the PHP community. While most people commented positively on the part that I’m proposing now. Or should I say really propose because the RFC, passed and was merged yesterday.

Derick Rethans 2:35

I had forgotten about it actually, it’s good that you reminded me. So yeah, it got merged and ready for PHP eight. Basically what you say you picked the non controversial parts of an early RFC?

Max Semenik 2:47

I actually chose something to contribute and then looked for an RFC, to see if it was discussed previously.

Derick Rethans 2:55

Oh, I see. So, your primary idea of wanting to contribute to PHP, instead of you having an itch that you wanted to scratch, it’s like you’re saying?

Max Semenik 3:04

I have way larger itches that I will scratch later when I will learn how to work with PHP’s code base which, which is really huge.

Truncated by Planet PHP, read more at the original (another 11910 bytes)

Book release: PHP for the Web – Matthias Noback

Book cover

While Advanced Web Application Architecture is still a work in progress, I decided to release another project in the meantime: a book for beginning PHP developers called PHP for the Web.

Of course, there are PHP books for beginners, but most of them aren’t very concise.
They cover many related topics like relational databases, CLI applications, and sending emails.
I wanted to write a book that only shows what makes PHP so special when it comes to web development.

Writing this book took me back to the beginning of my own programming career in 2002.
Many things have changed since then, but surprisingly many things are still the same too!

The biggest change when it comes to dealing with the web in PHP: we now have frameworks.
This is obviously a good thing, since it has made our applications much more secure.
For instance, in 2002 I wasn’t aware of the need for output escaping, or to escape parameters in my SQL queries, and I didn’t know that I should protect TinyMCE’s upload script against anonymous file uploads…

On the other hand frameworks hide so many of the lower-level details that I dare to say that many PHP developers today don’t know how it all works under the hood.
What are cookies and how do they work?
What’s the difference between PHP errors and exceptions?
What is $_FILES and how can I use it?

This new book, PHP for the Web explains it all in over 200 pages.

If you’re thinking about learning PHP to build web applications, use the following link to get the book for just $9: https://leanpub.com/learning-php-for-the-web-without-a-framework/c/RELEASE_DAY
And if you know someone who thinks about learning PHP, forward this link to them as well!

Here’s the table of contents to give you an idea of what’s in there.
If you want to read a sample, go to the landing page and click on Read Free Sample.

  1. Serving resources
    • Serving an index.html file with the built-in web server
    • The project root should not be the document root
    • Communication between the browser and the server
  2. Serving PHP scripts
    • The response: status, headers and body
    • Linking to other pages
    • Passing values between requests
    • user input can’t be trusted
  3. Forms
    • Submitting form data as query parameters
    • Always use output escaping
    • Adding a select element to the form
    • Submitting data via the request body
  4. Cookies
    • Setting a cookie
    • Using a cookie
    • Redirecting after processing a POST request
  5. Sessions
    • Session files and serialized data
    • Flash messages
    • Using flash messages everywhere
  6. Authentication
    • Setting up a login form
    • Logging out
  7. Project structure
    • Header and footer snippets
    • Bootstrapping
    • Routing
  8. CRUD part 1: Create
    • Saving JSON-encoded data in a file
    • Adding a tour
    • Form validation
    • Listing tours
  9. CRUD part 2: The rest
    • Introducing some reusable elements
    • Editing tour data
    • Deleting tours
  10. File uploads
    • Uploading a file
    • Showing the uploaded picture
    • Form validation for file uploads
  11. Error handling
    • Producing an error
    • Using different configuration settings in production
    • PHP errors
  12. Automated testing
    • Using Composer to install testing tools
    • Creating our first browser test
    • Starting with a clean slate
  13. Conclusion

Now go get your copy!

PHP Internals News: Episode 57: Conditional Codeflow Statements – Derick Rethans

PHP Internals News: Episode 57: Conditional Codeflow Statements

In this episode of “PHP Internals News” I chat with Ralph Schindler (Twitter, GitHub, Blog) about the Conditional Return, Break, and Continue Statements RFC that he’s proposed.

The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode’s MP3 file, and it’s available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news

Transcript

Derick Rethans 0:17 Hi, I’m Derick, and this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 57. Today I’m talking with Raluphl Schindler about an RFC that he’s proposing titled “Conditional return break and continue statements”. Hi Ralph, would you please introduce yourself.

Ralph Schindler 0:37 Hey, thanks for having me Derick. I am Ralph Schindler, just to give you a guess the 50,000 foot view of who I am. I’ve been doing PHP for 22 years now. Ever since the PHP three days, I worked in a number of companies in the industry. Before I broke out into the sort of knowing other PHP developers I was a solo practitioner. After that I went worked for three Comm. And that was kind of a big corporation after that I moved to Zend. I worked in the framework team at Zend and then after that, I worked for another company based out of Austin for friend of mine Josh Butts. That offers.com, we’ve been purchased since then by Ziff media. I’m still kind of in the corporate world. Ziff media owns some things you might have heard of, PC Magazine, Mashable, offers.com. The company that owns us owns is called j two they are j facts. They keep buying companies, so it’s interesting I get to see a lot of different products and companies they get bought and they kind of get folded into the umbrella, and it’s, it’s an interesting place to work. I really enjoy it.

Derick Rethans 1:39 Very different from my non enterprise gigs

Ralph Schindler 1:43 Enterprise is such an abstract word, and, you know, it’s kind of everybody’s got different experiences with it.

Derick Rethans 1:49 Let’s dive straight into this RFC that you’re proposing. What is the problem that this RFC is trying to solve?

Ralph Schindler 1:54 This is actually kind of the bulk of what I want to talk about, because the actual implementation of it all is is extremely small. As it turns out it’s kind of a heated and divided topic, My Twitter blew up last weekend after I tweeted it out, and some other people retweeted it so it’s probably interesting. I really had to sit down and think about this one question you’ve got is what is it trying to solve. First and foremost, it’s something I’ve wanted for a really long time, a couple years.

Two weekends ago I sat down and it was a Saturday and I’m like, you know what I haven’t haven’t hacked on the PHP source in such a long time. The last thing I did was the colon colon class thing, and I was like seven or eight years ago. And again, I got into that because I really wanted the challenge of like digging into the lexer and all that stuff and, incidentally, you know, I load PHP source in Xcode, and my workflow is: I like to set breakpoints in things, and I like to run something, and I look in the memory and I see what’s going on and that’s how I learned about things. And so I wanted to do that again. And this seemed like a small enough project where I could say, you know this is something I want to see in language, let me see if I can hack it out. First and foremost, I want this. And, you know, that’s, it’s a simple thing.

So what is it exactly is, it’s basically at the statement level of PHP, it is a what they like to call a compound syntactic unit. Something that changes the statement in a way that I think probably facilitates more meaning and intent, and sometimes, not always, it’ll do that and fewer lines of code. To kind of expand on that, this is a bit of a joke but a couple

Truncated by Planet PHP, read more at the original (another 22164 bytes)