I'm not convinced by Enums having methods. For those of you using other languages that support this, do you find them useful?
Modern PHP Cheat Sheet
71–80 of 127 posts
Re: Modern PHP Cheat Sheet
#72Earlier quoted context omitted.
It's not clear to me what you're looking for. But you can cast arrays to objects. And you can use type declarations as arguments for functions.
Ok say I have a small class with an options object (I know perhaps not the best design...): class Foo { public function __construct(string $name, array $options) { $this->name = $name; if ($options->enableFlag) { ... I want to declare what $options are, in TS I could do: type TFooOptions = { enableFlag: boolean; userIds: number[]; } Then function __construct(string $name, TFooOptions $options) ... Otherwise how do I…
is that creating a JS class under the hood or is just a hint for the compiler?
What I do , but I don't work with recent PHP versions is to create this as a class, then to make this easy to construct objects from JSON i add a static function fromObject or fromJson that does it for me. Using real classes could prevent bugs when working with shit APIs like Microsoft TTS , where they have 2 endponts and one returns objects like voice: {name:"Bob"} and the other voice: {Name:"Bob"} so my code will wrap both type of responses into a well defined class.
I don't think there is a PHPDoc annotation just to hint what that object looks like but I will be happey to be shown a way.
Re: Modern PHP Cheat Sheet
#73I like how PHP is evolving. I wouldn't say it's always a joy to use but it definitively improved a lot.
I really want to use PHP for one-off CGI scripts, (rsync files and done, deployed), but I really wish it had methods on primitive types. It hinders discoverability. It's annoying having to look up which function I need to use, and to find that there are many alternatives, most of which are deprecated. PHP stdlib needs an overhaul, IMO.
Re: Modern PHP Cheat Sheet
#74Earlier quoted context omitted.
Not only shipping things faster but testing faster, scaffolding faster, debugging faster ... the tooling for PHP is much more mature than it was 6-years ago and there are so many GOOD packages out there that haven't been forked and copied 20x over. Really happy to be working with it again, even if on the side for now.
I agree. Developer speed does not get enough coverage when comparing languages. When I work with a Node project it's such a slow dragging build process. Using PHP is like a fun superpower that lets me turn out things on timescales that are borderline miraculous to clients and management.
I can hire a top-notch PHP engineer for half of what a mid-level JS engineer would be asking.
It's not that PHP engineers are less qualified than JS. I think bootcamps shifted the market for JS developers and now really good engineers cost an arm and a leg while good junior engineers start around the $100k mark. The whole JS salary market is insane and as a startup, I can't afford it right now.
Re: Modern PHP Cheat Sheet
#75Earlier quoted context omitted.
Not only shipping things faster but testing faster, scaffolding faster, debugging faster ... the tooling for PHP is much more mature than it was 6-years ago and there are so many GOOD packages out there that haven't been forked and copied 20x over. Really happy to be working with it again, even if on the side for now.
I agree. Developer speed does not get enough coverage when comparing languages. When I work with a Node project it's such a slow dragging build process. Using PHP is like a fun superpower that lets me turn out things on timescales that are borderline miraculous to clients and management.
Re: Modern PHP Cheat Sheet
#76Earlier quoted context omitted.
It's not clear to me what you're looking for. But you can cast arrays to objects. And you can use type declarations as arguments for functions.
Ok say I have a small class with an options object (I know perhaps not the best design...): class Foo { public function __construct(string $name, array $options) { $this->name = $name; if ($options->enableFlag) { ... I want to declare what $options are, in TS I could do: type TFooOptions = { enableFlag: boolean; userIds: number[]; } Then function __construct(string $name, TFooOptions $options) ... Otherwise how do I…
class TFooOptions
{
public function __construct(
public bool $enableFlag,
public array $userIds = [],
) {
}
}
$options = new TFooOptions(
enableflags: true,
userIds: [1,2,3]
);
new Foo('Bob', $options);
Or if just for IDE checking, you could use the @property docblock value on the class.Re: Modern PHP Cheat Sheet
#77Re: Modern PHP Cheat Sheet
#78A Reddit post from a few years ago explains pretty well why PHP has such a terrible reputation: https://www.reddit.com/r/webdev/comments/5tqoqa/web_devs_wha... PHP should have chosen to undergo a rebranding a few years ago, if you ask me. The stigma it has to deal with is probably unsurmountable and will be forever. If you think PHP, you think WordPress and Joomla, and that means "hacks and vulnerabilities"... It's a…
if (md5('240610708') == md5('QNKCDZO')) {
echo "true\n";
}
Though I do use php, and love many things about it.Re: Modern PHP Cheat Sheet
#79What is modern PHP?
The opposite is PHP5-style code from the early 2000s, no package management, no type strictness, no object orientation, just a giant spaghetti mix of JS, HTML, SQL and PHP in a 10kloc file.
Re: Modern PHP Cheat Sheet
#80The comment on Numeric values is misleading: > Use the _ operator to format numeric values: > $price = 100_10; > // $100 and 10 cents Makes it sound like the underscore replaces the decimal, which it doesn't. The underscores are simply ignored by the PHP parser, so that 10_0.1 === 1_00.1. You could afterwards use PHP to replace the `_` with a comma for display purposes, and I assume that is what they are trying to sa…
Numbers can contain underscores for readability purposes.
These do not affect the value of the number, but can help read large numbers
more easily.
141592654 # how large is this number?
141_592_654 # ah, it's in the hundreds of millions!
Like all `Readability` issues, this one is not a technical concern.
But you can improve the odds of others reading and liking your code by making
it easier to follow.
Should make it more obvious that it is purely a readability thing, and that is obviously subjective anyway[1] https://github.com/rrrene/credo/blob/master/lib/credo/check/...