PHP tips

A pot-pourri of 14 PHP tips that were published with @exakat account.


When integer overflows

Mathematics have the ‘Ramanujan Summation’, where the sum of all integers is -1/12.

#PHP has the integer overflow. Stay away from the PHP_INT_MAX limits. Valid with (int) or intval() with recent #PHP versions.


Tweet


PHP recycles

TIL PHP recycles the previously created stdClass objects. The following code returns Object #1, until it is stored in $b.

TIL (2) : PHP’s stdClass’s constructor omits all its arguments.


Tweet


No force to string with objects

TIL #PHP needs to force the index’s type to make this code work.

It looked like fun code, though.

Of course, it is documented.


Tweet


PHP silent optimisation

PHP optimisation in action : undefined variables are only reported when they are used.

first is omitted : no operation
second is skipped : no need to execute 2nd term
third is reporting a warning.


Tweet


PHP keywords and namespaces

I was today’s old when I realized that #PHP keyword are allowed in namespaces name since #PHP 8.0.

This goes to PHP 8.0 Compatibility ruleset.

Tweet


object is not a type

Such situations always make me smile, yet I am certain several of us will loose time on such a mistake.

Tweet


__invoke() and properties and methods

What does this tricky #PHP code displays?

Tweet


Method syntax and __invoke()

There are some other cases around instanceof, which are surprising upon first read.

We can use a string in a variable, but not a direct string, a constant nor a ::class.

Tweet


strict_types exceptions

strict_types do not apply to #PHP operators, only on to typed structures.

Here, concatenation and interpolation all call __toString(), but not foo().

As you can see, print() and echo() are safe too, while implode() is not.


Tweet


Readonly invasion

#PHP 8.1 readonly properties cannot be set from global space, but they can be forced from the host class, just like accessing private properties.

It doesn’t work outside the host class : not in global space, not in a derived class.


Tweet


Variadic are typed and defaulted


Tweet


One call is not possible

Trap of the day : one of the calls in bar() will generate a ‘Non-static method a::foo() cannot be called statically’ error.

Which one? It is the d::foo(). All other calls are made within the C class : internal calls may use static or normal syntax, while external calls must use the correct call syntax. This allows calls like ‘parent::__construct()’.

When the call to bar() is made with ‘(new d)’, the ‘d::foo()’ works again.


Tweet


Where to get an elephpant?

When you need an elephant in your text, and you have #PHP handy :

its unicode is 128024 or 0x1F418


Tweet


Lint functions, not classes

#php lint detects early to avoid ‘redeclared functions’, based on local compilation.

php -l => Cannot redeclare mb_substr()

It doesn’t apply to CIT until execution though :

php => Cannot declare class stdClass, because the name is already in use


Tweet