@b_viguier@phpc.social avatar

b_viguier

@b_viguier@phpc.social

Developer @lendable // print_r($this) // TODO: update this comment France - Lyon https://b-viguier.github.io

This profile is from a federated server and may be incomplete. Browse more on the original instance.

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

TIL: in , you can create a new instance (not a clone) of an object from an existing instance with the following syntax:

$a1 = new A();
$a2 = new $a1();

Very convenient with anonymous classes! Just a bit confusing if you expect $a1() to call the A::__invoke magic method (because it won’t)

b_viguier,
@b_viguier@phpc.social avatar

@sarah @theseer I do agree! This syntax is maybe more explicit:

$a2 = new ($a1::class)();

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

Took me the whole week-end to understand a nasty bug involving destructors… It’s an @Xdebug feature: when an exception is thrown, a reference to local variables is kept (preventing destructors to be called)

https://xdebug.org/docs/develop
https://bugs.xdebug.org/view.php?id=2222

Tricky 😅

TL;DR: I removed the ‘develop’ mode, and it’s now working as excepted 👍

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

Reminder: use a .gitattributes file in your package to reduce the size of the downloaded archive
More details in this nice post from PhpWatch : https://php.watch/articles/composer-gitattributes

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

b_viguier,
@b_viguier@phpc.social avatar

@dseguy I guess [[]] is an edge case hard to exclude from the « generic » rule? Because something like this is valid and doesn’t produce any warning:

var_dump([][![]] ?? 'default');

🤷

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

TIL get_defined_vars function
https://www.php.net/manual/en/function.get-defined-vars
Can be useful to create “with-ers“ in an immutable (value) object with a LOT of readonly properties:

function withA($a):self {
return new self(...(get_defined_vars()+get_object_vars($this)));
}

https://3v4l.org/XhG90

b_viguier,
@b_viguier@phpc.social avatar

@Crell absolutely! And that « with-er » argument has the same name that corresponding internal property. But that’s a reasonable convention for a « basic » value object IMHO. At least, it is in my case 😉

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

It may sound silly, but I just wrote my first Quine (in of course), and it makes me happy 😊

<?=(fn($s)=>"<?=$s(\x27$s\x27)?>")('(fn($s)=>"<?=$s(\x27$s\x27)?>")')?>

https://3v4l.org/3QWr0

Crell, to php
@Crell@phpc.social avatar

Is there a function that would help replace or simplify a function with this description?

Creates a subarray from a given array based on a set of selected fields

@param $existingArray
eg: ["name" => "John", "age" => 25, "address" => ["city" => "New York", "zip" => "10001"]];

@param $selectedFields
eg: ["name", "address" => ["city"]];

@return

  • eg: ["name" => "John", "address" => ["city" => "New York"]];

It feels screwy, but I'm not sure what the better alternative is.

b_viguier,
@b_viguier@phpc.social avatar

@Crell I love this kind of challenges with arrays 😇

Here is my proposal: https://3v4l.org/ZCckm

Definitely not a « single » native function, but a combination of array_* and one anonymous function for recursion. I do NOT recommend to use this piece of code 😅

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

I recently discovered on iOs: https://www.phpwin.org/
This app is able to run a server on your iPhone/iPad, that’s awesome! I can now revive my old iPad 3 (iOs9!!) with some (crazy) Php projects 🥳
I do recommend 👍

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

Could you improve an expression such as '$a == 'a' || $a == 'A'' ?

I collected 12 variations of that challenge.

in_array() is the surprising winner of that race, with strcasecmp() and isset() in tow. || (or) is not far and a good solution in any case.

https://www.exakat.io/en/unraveling-the-quest-for-the-fastest-case-insensitive-char-comparison-in-php/

b_viguier,
@b_viguier@phpc.social avatar

@dseguy Really interesting!

Sorry, I’m late, but what about a « match » expression?

match($char) {'a','A'=>true, default=>false}

My guess is that it should be close to in_array with strict comparison 🤔

And about the « isset » test, do you include the intermediary array creation inside the loop? Is the array also created 10M times?

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

What will display this code?

No idea why someone could want to do that… but still interesting 😅

(Answer 🤫 https://3v4l.org/i7lf7#v8.2.11 )

b_viguier,
@b_viguier@phpc.social avatar

@dseguy Yes, as soon as your function contains ‘yield’, it returns a Generator. But in the constructor, the created Generator seems unreachable, exactly like if you try to return a value from the constructor…

It definitely makes sense, but it’s surprising 😉

b_viguier,
@b_viguier@phpc.social avatar

@dseguy As soon as a function foo contains the yield keyword, even if unreachable, the foo function won’t be actually called. It returns a Generator that will call foo if you iterate over it. To retrieve the value actually returned from a Generator function, you have to use Generator::getReturn
https://www.php.net/manual/en/generator.getreturn.php

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

« ReactPhp, AmPhp, RevoltPhp: Asynchronous Frameworks Comparison »
Slides of my talk at

https://b-viguier.github.io/downloads/talks/ForumPhp2023-AsynchronousFrameworks.pdf

b_viguier,
@b_viguier@phpc.social avatar

@thgs Thanks 🙂

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

Today I played with extension php-meminfo, to track some « lost » variables causing memory leaks: https://github.com/BitOne/php-meminfo

This tool is awesome! 🤯 A must have when dealing with memory issues 👍👏

b_viguier,
@b_viguier@phpc.social avatar

@Pol I used master, and it worked with Php 8.1. Just a small bug with the utf8 encoding in the produced json file, but I found a workaround in an existing issue (maybe related to my codebase)

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

A small trick, combining named parameters, spread and union arrays operators to « easily » create a modified copy of a DTO: https://3v4l.org/ZWX5G#v8.2.10

⚠️ It’s fun if you have a lot of parameters, but using a string containing the parameter’s name isn’t really satisfactory 😕

b_viguier,
@b_viguier@phpc.social avatar

@dgoosens Ho yes, you’re right!
TBH this is what I had in mind, but I often mix both terms 😅

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

In , remember that « readonly » doesn’t necessarily imply immutability ⚠
Objects and references can be tricky…

https://3v4l.org/9ftoX#v8.2.9

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