dseguy, to random French
@dseguy@phpc.social avatar

If you forgot in which PHP version a feature was introduced, https://caniphp.com/ did not forget so you can check it there.

dseguy, to php French
@dseguy@phpc.social avatar

😂 This made me chuckle... welcome to 8.3 !

First array is the type, the second is the name of the constant, with the relaxed keyword, and the last one is the value of the array.

dseguy, to php French
@dseguy@phpc.social avatar

Comparison of memory usage and execution speed between associative arrays, named and anonymous classes, stdClasses and extended classes, across PHP versions.

TLDR; using classes is more memory efficient, and a bit faster.

https://www.exakat.io/en/array-classes-and-anonymous-memory-usage/

dseguy, to random French
@dseguy@phpc.social avatar

Choose your team : condition in a loop, or array_filter() first ?

Make up your mind, and then you can read this new blog post.

The result will NOT surprise you.

https://www.exakat.io/en/array_filter-versus-loop-condition-checks/

dseguy, to php French
@dseguy@phpc.social avatar

The (array) operator is a simple cast to array. Yet, it is one of the less obvious feature of the language : (array) on integers, on null, on objects, recursive and hidden.

That's a lot to discover for the oldest and most useful operator.

https://www.exakat.io/en/mastering-the-array-cast-operator-in-php-a-comprehensive-guide/

dseguy, to random French
@dseguy@phpc.social avatar

To make a class alias, use class_alias();
To make an interface alias, use class_alias();
To make an enum alias, use class_alias();
To make a trait alias, use class_alias();

Also, no ::enum operator, ::trait operator?

dseguy, to php French
@dseguy@phpc.social avatar

Here is another way to make recursive calls in : use the generator delegation.

The main difference is that it needs to be in a foreach() or a generator using function.

https://3v4l.org/2FDXY

dseguy, to php French
@dseguy@phpc.social avatar

Useless tip of the day:

Null and booleans accept the array syntax, but always return NULL (and a warning)

Illegal types for array keys lead to a Fatal Error.

But not with NULL and booleans.

https://php-tips.readthedocs.io/en/latest/tips/nullAsArray.html

dseguy, to php French
@dseguy@phpc.social avatar

foreach() works on arrays and objects. On objects, it skips uninitialized properties, which may be very convenient with readonly properties : otherwise, it is a fatal error!

https://php-tips.readthedocs.io/en/latest/tips/foreach_skips_uninitialized.html

dseguy, to php French
@dseguy@phpc.social avatar

A quick review of what has changed in switch() with 8.

switch() is impacted by the integer / string comparison evolution, and also by special tricks with null. All these are detailled in this article. Lots of

https://exakat.io/en/php-8-changes-in-switch/

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

In the list below, there is one fatal error. All others are working as expected, except for the fact that they probably should yield an error and be not legit.

The error says : "Cannot use [] for reading".

Did you guess the one?

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

One of the two var_dump() here is not the same.

Unsetting properties is a always a bad code smell.

dseguy, (edited ) to php
@dseguy@phpc.social avatar

What is this piece of code will display in 8.2 ?

<?php

enum x: string {
case B = 'b';
const C = 'c';
}

class x2 {
const E = x::B->value;
const F = x::C + [];
}

echo x2::E;
?>

dseguy, to php French
@dseguy@phpc.social avatar

yield keyword allows anything as key, and that is transmitted to the calling foreach() command. So, you can have arrays or closures as keys, or worse.

Just a weird use case for tonight.

https://3v4l.org/o8jBb

dseguy, to php French
@dseguy@phpc.social avatar

Cunning Little Switch : classic improvements to writing the old and simple switch in .

Some are errors, others are tips : all are worth keeping in mind.

https://www.exakat.io/en/well-structured-switch-command-in-php/

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

Friday Heart Attack :

What does this code will display ?

😜

dseguy, to php French
@dseguy@phpc.social avatar

8.2 has the #[SensitiveParameter] attribute that hides values in debug messages.

There is also the SensitiveParameterValue class, that does the same, while working from the caller perspective. Get the value with $object->getValue()

https://3v4l.org/KrViH

https://www.php.net/manual/en/class.sensitiveparameter.php

dseguy, to php French
@dseguy@phpc.social avatar

How to clean after oneself in ?

There are three methods to ensure that this little cleanup code is always executed, no matter what : register_shutdown_function(), function __destruct() and try-finally.

https://www.exakat.io/en/how-to-clean-after-oneself-in-php/

dseguy, to random French
@dseguy@phpc.social avatar

TIL that it is possible to redefine PHP functions and constants, using the 'use' command.

This is not the case for classes (or any CITE)

https://php-tips.readthedocs.io/en/latest/tips/redefine_native_functions.html

dseguy, (edited ) to php French
@dseguy@phpc.social avatar

It is possible to put 2 elements in a array, find different 5 keys with array_key_exists or isset) and yet, still count 2 distinct elements (key wise).

The type-juggling for array keys is applied in every features, to keep things easy to use.

This code is one rare way to show how it still leaks. Depending on the context, it might be very confusing.

https://3v4l.org/hYsN9

dseguy, to php French
@dseguy@phpc.social avatar

Quizz : when is it useful to add the same trait t in a parent AND in a child class ?

dseguy, to php French
@dseguy@phpc.social avatar

I finally found an occurrence of this beauty :

$this->$this

It still looks like a typo in the original code, but with the help of __toString, it works. Also, strict_types doesn't help here.

https://php-tips.readthedocs.io/en/latest/tips/this_this.html and more tips!

dseguy, to php French
@dseguy@phpc.social avatar

In this code, complains about an unexpected semi-colon, which... doesn't exist.

It is actually the closing tag, which acts as a end of command.

via Ryan Chandler

https://php-tips.readthedocs.io/en/latest/tips/no_semi_colon_in_sight.html

dseguy, to php French
@dseguy@phpc.social avatar

So, you have a variable with the name of something (a class, a method, arguments, etc) and you want to call it?

has your back. You can call about anything. There's always a syntax for a dynamic call.

https://www.exakat.io/en/all-the-dynamic-syntaxes-in-php/

dseguy, to random French
@dseguy@phpc.social avatar

TIL file_put_contents() accepts an array as second argument, and will do an implode("", $array) on it when writing.

Who is using this feature?

cc /@Girgias

https://php-tips.readthedocs.io/en/latest/tips/file_put_array.html

  • All
  • Subscribed
  • Moderated
  • Favorites
  • anitta
  • GTA5RPClips
  • osvaldo12
  • thenastyranch
  • DreamBathrooms
  • khanakhh
  • magazineikmin
  • InstantRegret
  • Youngstown
  • slotface
  • mdbf
  • love
  • kavyap
  • rosin
  • megavids
  • everett
  • cubers
  • ethstaker
  • Durango
  • ngwrru68w68
  • tacticalgear
  • modclub
  • cisconetworking
  • Leos
  • provamag3
  • normalnudes
  • tester
  • JUstTest
  • All magazines