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 )

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

TIL that the cast operators such as (int), (array)... are case insensitive: (STRING)

Also, the amount of white space (space, tabs...) is not important.

The rest is coding convention, as they say.

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

When refactoring arrays into object, you can count on 's dynamic syntax to keep all your code working while replacing Plain Old Arrays by classes.

Learn how to harness PHP dual syntax array-objects!

https://www.exakat.io/en/smooth-migration-from-array-to-object/

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

I noticed only recently that array syntax on object are Fatal, while object syntax on array are only a Warning and a NULL.

This looks like backward incompatibility. May be could introduce a directive to level both errors?

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

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

Class invasion, it when an object access anything it wants in another object of the same class. Thanks to the way checks each object's perimeter.

The same checks allow the other object to access the current one's, as seen in that example.

https://3v4l.org/9XWmu

https://php-dictionary.readthedocs.io/en/latest/dictionary.html#class-invasion

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

I think I expected $a (the second print_r) to show an array, but -3 is indeed more logical.

I also expected the static $x to be [-3], thanks to the reference, but it isn't.

Weird night coding.

https://3v4l.org/guEKP

What do you think ?

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

I'm still struggling to pick a side.

@ is too slow, because it merely hides the error.

?? looks dumb: it reads : if it is null, use null as default.

the if() command is long to type.

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, (edited ) to random French
@dseguy@phpc.social avatar

Untyped properties and properties typed as mixed are not compatibles one another.

Although, they represent the same typing : anything.

Same for return type, in method compatibility situations. Although, it happens only when the parent is mixed, and the child untyped.

https://3v4l.org/HkW9J

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/

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

Static is used to mark a property as 'static',
and as a type for that property.

Leading to those confusing lines below.

Static would accept self, but also parent and any children class: so, self is sufficient and there is no syntax for for 'static static'

https://3v4l.org/ffPcv

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

I know there is no such a function as empty(), as it is a language construct.

8.0 brought an improvement to this, as this is now a Fatal Error, rather than a Parse Error.

But, that aside, come on...

https://3v4l.org/ML6cX

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

@merms I like to take into account the types for that kind of problem : keys are of type string|int. When they are used with a string-only or int-only method (trim() here), a cast is necessary.

I have to say this is quite skewed towards string types, as PHP never juggles integers to strings.

Some 'integer strings' stay integers, though. 14 digits and longer, for example.

https://3v4l.org/IdZ2g

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

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

I think I found a 'void' parameter in . It is the second argument of array_keys().

That second parameter is often omitted (and unknown).

If present, it is typed 'mixed' to allow any value to be searched (here, null).

If absent, array_keys() returns ALL keys. When absent, it is not null, nor any other type. The last one possible is 'void'

Type is then : void|mixed.
Does that make sense or am I over-thinking this?

https://3v4l.org/VVDRW

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

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

Unsetting properties is a always a bad code smell.

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