Live data from Hacker News

Modern PHP Cheat Sheet

front-line-php.com

71–80 of 127 posts

Re: Modern PHP Cheat Sheet

#72
post #52

Earlier 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…

>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 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

#73
post #15
post #3

I 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.

[deleted]

Re: Modern PHP Cheat Sheet

#74

Earlier 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.

Hiring developers is rarely considered as well.

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

#75

Earlier 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.

There's an interesting comment/quote on an Hacker News post about Perl's "Laziness Impatience Hubris" mantra that I think of when people talk about developer speed.

https://news.ycombinator.com/item?id=23826828

Re: Modern PHP Cheat Sheet

#76
post #52

Earlier 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…

You can get some basic checking, but not type in arrays [1]. Maybe using attributes you could check if it is?

  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.

[1] https://wiki.php.net/rfc/arrayof

Re: Modern PHP Cheat Sheet

#78

A 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…

My personal favorite PHP wtf is this one:

  if (md5('240610708') == md5('QNKCDZO')) {
    echo "true\n";
  }
Though I do use php, and love many things about it.

Re: Modern PHP Cheat Sheet

#79

What is modern PHP?

It commonly refers to PHP 7+ paired with clean code practices and SOLID principles.

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

#80

The 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…

Yea, Credo[1], one of the static analysis tools for Elixir specifies it as:

  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/...

Post reply on HN