I wish PHP 9.0 had a feature called "Flag".

No more bitwise operators, just fluent flag declaration. I'll prefer that rather than adding numbers and then wondering what is "8 | 16".

#Programming #Code #Coding #PHP #SoftwareDevelopment #WebDevelopment #WebDev #PHP9

@darkghosthunter What you want are Enum Sets.

I want them, too.

@Crell Like manually adding 2, 4, 8, 16, 32, 64...? I mean, I don't want everyone to memorize that. I would prefer having a Flag declaration.
@darkghosthunter No no. Declare an enum, and then a Set that holds 1 or 0 of each enum case.
@Crell But you still have to do bitwise operations, or I'm wrong?
@darkghosthunter Depends on the implementation. You wouldn't need to remember magic values either way.
@Crell Yes, but I wouldn't want users to understand how bitwise operators work for just one function.

@darkghosthunter On phone atm, so can't easily access the link where I lay out the ideal setup.

But there's noting inherently wrong bitwise operators. Ideally they're not the only option, but they really are an efficient solution. Even with named values instead of magic numbers.

@darkghosthunter

Consider this summary of operators/methods: https://github.com/Crell/php-rfcs/blob/master/collections/research-notes.md#set-5

With these classes:

abstract class Set<T> {}

class FileMask extends Set<FileFlag> {}

$fm = new FileMask();
$fm->add(FileFlag::Read);
$fm += FileFlag::Write;
$fm & FileFlag::Write; // true
$fm & FileFlag::Exec; // false
$fm->has(FileFlag::Read); // true

That's ~ what I want. (The doc is a few years old, rambling research I did for the Foundation.)

@Crell That ~ looks sick. Personally, I light more straightforward implementations. Meanwhile, I'll have to create a class with a `toFlags()` to convert it to a binary.

```
$flags = new JsonFlags();

$flags->prettyPrint()->zeroFloat();

json_encode($array, $flags->toFlags());
```

@darkghosthunter Hard to get more straightforward than a basic Set structure that happens to hold enum values. :-)
@darkghosthunter I don’t get it? You can just use constants or enums for this. I don’t even understand how to read your code. What does the property access on the case do? Instead of understanding bitwise operators I need to understand a different concept now.