kboyd, to random
@kboyd@phpc.social avatar

const bull = null;

🙊

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

allows to use 'class' as a class name in a type. Same for interface, enum... and some others.

Hey! I'm also not too proud of myself, but it made me chuckle.

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

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

has a 'precision' directive that controls the number of decimals displayed by float values.

It default to 14, and peaks at 18, and gives up at 54. Use gmp for large numbers.

When changing it, don't forget to reset it to default, unless you like to puzzle people.

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

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

doesn't allow float as index in arrays.

It is possible to use them as such by casting them to string. Later, type juggling will allow adding to another float, seamlessly.

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

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

has (int) and (integer) cast operators, but only the int type.

'integer' type is presumed to be a class. It raises a warning, even if it exists.

"integer" will be interpreted as a class name. Did you mean "int"? Write "\integer" to suppress this warning

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

A variadic argument collects all the unused named parameters, along with their key. That way, it is possible to handle them with their name inside the method.

On the other hand, array_merge (and some cousins) refuse them, and emits a Fatal error.

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

What is a
function which has a return type, yet doesn't return anything?


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

The #PHP code below produces a float, although it seems to produce an integer.

This is due to (int) having higher precedence than multiplication: it is applied to $c, without effect.

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

#phptip #phptrick

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 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

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

Reminder that namespaces are not nested, but are only prefixes.

There is the current namespace, and the global one, for the occasional fallback. Nothing in between.

Namespaces are not folders, even when they are stored as such.

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

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, to php French
@dseguy@phpc.social avatar

allows a constant to be called 'int', but it will then mistake it with the (int) cast operator as soon as it is between parenthesis.

The code below is a fatal error!

And this applies to all the other cast operators.

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

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

abstract logic :

A concrete class can be made abstract again.

A concrete method cannot be made abstract again.

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

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

How to clean after oneself in #PHP ?

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.

#phptip #phptrick

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

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

Here is a very optional #PHP command : try-catch-finally

The finally clause in a try-catch-finally is optional: it can be omitted and often is.

The catch clauses in a try-catch-finally are also optional: they can be omitted.

When the catch and finally clauses are all omitted, the try clause can also be omitted. Safely.

#phptip #phptrick

https://php-tips.readthedocs.io/en/latest/tips/try-catch-finally.html

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

Useless tip of the day:

#PHP 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.

#phptip #phptrick

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

heiglandreas, (edited ) to random
@heiglandreas@phpc.social avatar

TIL that

$a = set_error_handler('my_error_handler');
set_error_handler($a);

is something different than

set_error_handler('my_error_handler');
restore_error_handler();

🤯

#php

Edit: Fixed typos

Exakat,
@Exakat@phpc.social avatar

@heiglandreas @bhhaskin One is missing a 'L' 😇

If the built-in error handler is used null is returned. So, you can't reset the handler by giving it the returned value of the first call. Quite surprising, indeed.

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

I scream, you scream, we all scream for @ no scream operator: a usage review of the @ operator in code, when to use it and when not to use it.

https://www.exakat.io/en/i-scream-you-scream-we-all-scream-for/

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

#PHP is better than me : I thought I found a way to create properties in an interface, by declaring promoted properties in a constructor.

Someone coding the PHP engine was smarter than me : it feels so good! (BTW, who would that be?)

It's also going to save me some sanity, as I don't have to upgrade my Static Analysis Engine.

#phptip #phptrick

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

TIL you can get the following error message :

"Cannot add element to the array as the next element is already occupied"

How?

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

Quizz question :

Can you write a function that won't execute when you call it?

(Just change the comment // more code )

  • All
  • Subscribed
  • Moderated
  • Favorites
  • tester
  • everett
  • magazineikmin
  • thenastyranch
  • Youngstown
  • Durango
  • rosin
  • slotface
  • InstantRegret
  • mdbf
  • PowerRangers
  • kavyap
  • tsrsr
  • DreamBathrooms
  • Leos
  • ngwrru68w68
  • hgfsjryuu7
  • cubers
  • GTA5RPClips
  • khanakhh
  • vwfavf
  • ethstaker
  • osvaldo12
  • cisconetworking
  • normalnudes
  • tacticalgear
  • modclub
  • anitta
  • All magazines