Posts

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

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

En route for (Karlsruhe) with the elephpant patriarch and an advanced session on types in . See you there!

flowcontrol,
@flowcontrol@phpc.social avatar

@dseguy see you there

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

question of the day : when you know that
2*2 = 4
2^2 = 4 (too)
2.5^2.5 = 9.8821176880262..

Where is this 6 coming from ?

dseguy,
@dseguy@phpc.social avatar

@derickr The first print shows 4, the second one says 6.

dseguy,
@dseguy@phpc.social avatar

@JerryWham The first print shows 4, the second one shows 6.

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

More about the Override attribute :

where else can use this attribute?
when will it raise a Fatal error?
It is not always at compile time (it never is).

https://www.exakat.io/en/more-about-the-override-attribute/

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

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

#phptip #phptrick

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

mitelg,
@mitelg@phpc.social avatar

@dseguy works "seamlessly"? I guess, it will also lose information due to the float precision issues in some cases 😬

dseguy,
@dseguy@phpc.social avatar

@mitelg mea culpa, it was seamless in the case I was working on.

With a finite float, it works.

With calculated float, there are possible hidden decimals : https://3v4l.org/dJ0mP;

It will loose precision with the default 'precision' (the directive), but not with higher levels (due to truncation).

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

Thursday, April 11th is @010php day, hosted by @bitfactory.

This month's session is about advanced type usage. See the impact of types on the structure of your code, identify impossible methods and design the others!

Go! https://www.meetup.com/010php/events/299863925/

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

@heiglandreas 😂 :elephpant:

edorian,
@edorian@phpc.social avatar

@dseguy I've done worse tbh. And some of it turned out to be a good idea for very specific circumstances.

Yet to find a sensible reason to use toString though.

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.

ocramius,
@ocramius@mastodon.social avatar

@heiglandreas @gmazzap @dseguy ye, but you will need to wait another 2 decades for a language level fix 😛

The SA stuff is all opt-in-or-I-told-you-so, so the alternative is jumping to another language.

heiglandreas,
@heiglandreas@phpc.social avatar

@ocramius Because BC Breaks.... 🤪

It's a f*cking Bug!

/cc @gmazzap @dseguy

dseguy, to php French
@dseguy@phpc.social avatar
  1. it is possible to call a method 'as', because relaxed keywords methods

  2. it is possible to import a method from a trait, and alias it to itself.

It is possible to write 'as as as ' in and make sense.

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

dseguy,
@dseguy@phpc.social avatar

@alessandrolai 😮 Aliased constructor...!! I might have overlooked that one.

alessandrolai,
@alessandrolai@phpc.social avatar

@dseguy yeah I needed to mimic parent::__construct, that is not doable with plain traits 😂

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

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


dseguy,
@dseguy@phpc.social avatar

@heiglandreas yield is not typed in .

That's one of the places where type system is lacking.

ramsey,
@ramsey@phpc.social avatar

@dseguy @heiglandreas It yields (returns) a type of Generator, right?

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

A story of compact() and extract()

Compact() and extract() are two sides of the same coin. They are also a good part of the PHP story, along with the variable variables. Let’s see how they can make it into the future of PHP.

https://www.exakat.io/en/a-story-of-compact-and-extract-2/

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

The 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

velkuns,
@velkuns@phpc.social avatar

@dseguy same for ternary operator. That's why explicit things like ( ) is recommended to avoid problems 😅

dseguy,
@dseguy@phpc.social avatar

@velkuns Yes, I can do a long list of those operators... Fun with precedence!

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