Live data from Hacker News

Modern PHP Cheat Sheet

front-line-php.com

121–127 of 127 posts

Re: Modern PHP Cheat Sheet

#121
post #34

Genuine question from a php hobbyist : what is the equivalent of Typescript’s ability to declare an object’s structure? It’s really weird to me, I mean don’t we do this all the time? Work with eg. an $options array/obj passedto a constructor, or say, a message decoded from JSON… I could write $name = $message[‘username’] … and there is no checks in ide or runtime, while the phpdoc will just document $message to be an…

I feel like TypeScript and Flow went down the structural typing path (rather than relying on nominal types) because they were following what was popular in JavaScript at the time, not because it is an unequivocally better solution. The last time I programmed PHP, I didn't feel having to define lightweight classes was a big deal.

The examples you mentioned are interesting because I consider `options` arguments to be a code smell that tends to happen because TypeScript is missing named parameters, which PHP has. And typing JSON messages is far from a solved problem in TypeScript, because there is no widespread solution for runtime type checking.

Re: Modern PHP Cheat Sheet

#122
post #31

I'm using Laravel/PHP for a new project and it's been a joy . A true joy. After spending the last 6+ years writing backend services in Node, PHP feels like a breath of fresh air. No restarting the server, no compiling errors from babel/typescript wackiness, no blocking threads, server-side templates out-of-the-box, and so much more. PHP has come a really long way, I really hope to see it's resurgence some day after e…

Not my cup of tea, but if you really want PHP-like with JS, EJS looks really close to "I wish server-side javascript worked like PHP": https://ejs.co/ Just needs a FastCGI wrapper. Another "Node, but PHP like" project that compares itself to EJS and other similar things: https://github.com/Yahweasel/nodejs-server-pages#why-nodejs-...

[deleted]

Re: Modern PHP Cheat Sheet

#123

Earlier quoted context omitted.

>type TFooOptions 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…

Simple object type in TypeScript, the "type checking" abstraction layer on top of JS, which actually doesn't even need to be compiled (eg. esbuild strips all typing from the code before compiling). https://www.typescriptlang.org/docs/handbook/2/everyday-type... Your approach is sensible. I remember using json_encode() indeed so I could declare some data in a php file with <<< HEREDOC in the Javascript short syntax (t…

I checked that example in TS playground and it does nothing when transpiling, so is only a compile type check. From my experience when I have an option object with many properties I would create a class both in PHP and JavaScript, usually functions that need an options object are very complex and many times you need to add even more options and having a class make it simple to do the changes and refactor.

Re: Modern PHP Cheat Sheet

#124
post #97

Earlier quoted context omitted.

> People seem to love to hate it because it's the "cool" thing to do. While I agree with this, I think the tone is slightly dismissive. From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance, etc. in its history. Though I have (thankfully) seen that the language and its standard library has undergone several…

> It's why we get things like Deno, which attempt to throw all this cruft away and start again Doubt. Your "something@v1.2.3" in Deno is no different than "something": "^1.2.3" in npm. The problems will appear when you update stuff. The only thing benefiting deno currently is the clean slate and the lack of hacks (for now).

They are different, currently no Deno registries have dynamic version resolution (no greater than operator). Once you have locked in on a version you are in it for good unless you change it manually

However if the argument is: updating libraries break stuff, that is gonna happen every time in every language. There is no guarantee the next version will just work as the last one did

Re: Modern PHP Cheat Sheet

#125
post #91

My absolute favorite thing about PHP is the arrays and the ease of working with them. Whenever I find myself working in another language dealing with loads of data I'm immediately thinking of how easy it would be to accomplish it in PHP. And I've been loving all the new additions to PHP over the last few years as well. Version 5 was rather limited but since 7 it has grown into an actual programming language for which…

Is it really useful to have ordered map as the "only" collection type? I guess it might be Stockholm syndrome - but I'm quite comfortable with a "list/array" type (numeric keys, ordered) and a "map/hash_map" type (objects as keys). Especially give that with phps "power" you can: $array = array( 1 => "a", "1" => "b", 1.5 => "c", true => "d", ); var_dump($array); Ouputs: array(1) { [1]=> string(1) "d" } https://www.php…

That's indeed a case where it fails, but in all my years I have maybe ran into that problem once. But it's trivial to implement a more strict form of array if you wish. And if I remember correctly there are some standard extensions that have classes to do just that.

And for the record, it's not Stockholm syndrome, I use plenty of other languages and I know the benefits and drawbacks of each of those.

Re: Modern PHP Cheat Sheet

#126

Earlier quoted context omitted.

Do you know why that is? php > echo md5('240610708'); 0e462097431906509019562988736854 php > echo md5('QNKCDZO'); 0e830400451993494058024219903391

Would this be avoided by using a string comparison function? https://www.php.net/manual/en/function.strcmp.php

Yes, or you can use ===, which will check the type as well. I am trying to get in the habit of using === everywhere now.

Re: Modern PHP Cheat Sheet

#127
post #60

Earlier quoted context omitted.

How do you find those kickass ones on Fiverr among a sea of bad ones?

the good ones are not on fiverr ;)

The above comment says: "many of the good ones are from poor regions in the world and you can find them on Fiverr doing kick-ass work for a grand total of $50. "

That's what I'm replying to.

Post reply on HN