WeakReferences

Among the many wonderful improvements coming in PHP 7.4, are WeakReferences. What is a weak reference? To answer that question, we first need to quickly skim how references work in PHP. Strong references In PHP, objects are not stored in the variables they are assigned to. Instead, the variable stores a reference to that object. In this way, multiple variables can all point to the same object. Up until now, all references used in PHP are implicitly strong references.

PHP Keywords: namespace

Welcome to my series on every PHP keyword and its usage. Today’s item: namespace. The problem Every time you define a class, function or constant in the global scope, you define one more name that you can’t use again. This is typically fine in your own code, it’s easy enough to come up with a different name, if you’ve already used one before. But what about when you want to use someone else’s library?

PHP Keywords: instanceof

Welcome to my series on every PHP keyword and its usage. Today’s item: instanceof. The instanceof operator allows you to check if an object is an instance of a particular class (or a class that extends a particular class) or implements a particular interface. Most of the time you’ll directly check if an object implements a specific class, but you can also check it against a class name stored inside a variable.

PHP Keywords: goto

Welcome to my series on every PHP keyword and its usage. Today’s item: goto. The goto statement allows you to jump around in the current context, disregarding normal code flow. Using goto should be avoided in most situations, as it makes it harder to reason about your code. In PHP, goto is restricted to the current file, and the current context. This means that you can not jump to another file, nor can you jump into or out of a function.

PHP Keywords: do, while and endwhile

Welcome to my series on every PHP keyword and its usage. Today’s items: do, while and endwhile. The while loop is the simplest loop to use, and the easiest to mess up and trigger an infinite loop, since it does not encourage you to iteratively change the condition. Here’s a quick example: 1 2 3 4 5 6 7 <?php $i = 0; while($i < 10) { echo $i; } As you can see, we’ve forgotten to increment $i, causing an infinite loop.