Live data from Hacker News

Everything you need (and don't need) to know about PHP's type system

thephp.website

51–60 of 67 posts

Re: Everything you need (and don't need) to know about PHP's type system

#51

Earlier quoted context omitted.

Correct? I'd probably say predictable.

Eh. If MyPy complains that I'm passing an `Optional[Entity]` into a method where only an `Entity` can go, it's just helped me avoid a runtime error when I try to get `entity.name`. Not sure what the exact PHP equivalent is, but there's all sorts of bugs you can easily introduce if you're not careful with types.

PHP doesn't compile so all errors become runtime errors.

Re: Everything you need (and don't need) to know about PHP's type system

#52
post #21

Is there a good reason to still use php when you can use hacklang ( https://hacklang.org/ ) which has much stronger type system. Some would even call it php++.

A lot of what Hack introduced to the PHP community has become available in suitably forward-thinking ways to allow for a sensible amount of backwards compatibility and are opt-in. For example, you can set strict typing on a per-file basis. `declare(strict_types=1);` I believe that the performance difference between the two is negligible now too. So it really just comes down to personal preference/platform legacy. But…

the only thing that sucks about strict typing is that there's no way to toggle it globally. so if you want to transition a codebase that's simultaneously actively being developed in other ways, you have to mess around with scripts that append it to the start of all php files/remove it again

Re: Everything you need (and don't need) to know about PHP's type system

#53
post #51

Earlier quoted context omitted.

Eh. If MyPy complains that I'm passing an `Optional[Entity]` into a method where only an `Entity` can go, it's just helped me avoid a runtime error when I try to get `entity.name`. Not sure what the exact PHP equivalent is, but there's all sorts of bugs you can easily introduce if you're not careful with types.

PHP doesn't compile so all errors become runtime errors.

It is nice that you are trying to inform me but I am already aware of the information that you bring and your decision to bring it makes it seem to me as if you are uninformed. It is possible to conduct static analysis of a source code file that is not "compilation" in the sense that you would otherwise see with Java or C. Python's not really any different than PHP in this regard.

Re: Everything you need (and don't need) to know about PHP's type system

#54
post #18
post #12

> Important to notice that casting an array with numeric keys into an object is valid, but one can't dereference its value because property names may not start with numbers. Not true, actually. The (admittedly obscure) syntax to access a property beginning with a numeral is `$obj->{0}`. Usually when this happens it's because someone was trying to be clever with JSON.

There's another way to get a variable starting with a numeral - abusing extract(), I think.

I've seen a few workarounds, each worse than the last, including casting back to array, round-tripping through JSON with the array flag set, and reflection. Hence the public service announcement that this is perfectly doable with the correct syntax.

Re: Everything you need (and don't need) to know about PHP's type system

#55
post #51

Earlier quoted context omitted.

Eh. If MyPy complains that I'm passing an `Optional[Entity]` into a method where only an `Entity` can go, it's just helped me avoid a runtime error when I try to get `entity.name`. Not sure what the exact PHP equivalent is, but there's all sorts of bugs you can easily introduce if you're not careful with types.

PHP doesn't compile so all errors become runtime errors.

Yeah but should use static analysis tools in your CI/CD and development workflow when using an interpreted language. That's a must have.

So better typing can definitely help to catch those before pushing and running your code.

Re: Everything you need (and don't need) to know about PHP's type system

#56
post #49

Earlier quoted context omitted.

It's gotten a lot better since I started using it ~2009 and now. 7.4 added typed properties. 8 is adding better caching, a jit and my favourite feature match() https://php.watch/versions/8.0#match-expression for all the enterprise heavy stuff I write a nicer cleaner and safer (by default) alternative to switch is going to make some code much cleaner to write and read and really that is what I care about more than alm…

Match looks exactly how Rust’s match works and it’s one of my favourite parts of the language. Happy to see it elsewhere too.

It doesn't really pattern match like rust, but it's still very useful

Re: Everything you need (and don't need) to know about PHP's type system

#57
post #10

It actually makes me really happy to see more positive comments on a thread about PHP. It is an incredible workhorse, and doesn't get the credit it deserves. I'm often amused by developers that revile PHP, while going on to use another language, which suffers from a similar set of problems to PHP, oftentimes with poorer performance and more complex toolchains. PHP deserves a little more love, imo.

PHP has its special place for every developer that works on web. And it got all recent improvements thanks to all those criticisms. I think every popular language receives criticism and that's just fine.

I'm happy that the language is evolving and there is a strong ecosystem with quality libraries and developers unlike what it used to be 5 years ago.

I've been doing several languages (Scala, and Typescript. also Go recently) in last few years. But, I still follow PHP ecosystem closely and I'd definitely choose it for my next web startup. It's just 10x faster and ultimately cheaper to build web with PHP. That's why there are so many big success stories that started with PHP even in areas that you wouldn't believe. Surprisingly enough, not only web! One of Cloudflare's founders said on an interview that their back-end was written in PHP and it was used for a long time. That's one of the things that you think no one would do.

Re: Everything you need (and don't need) to know about PHP's type system

#58
post #57
post #10

It actually makes me really happy to see more positive comments on a thread about PHP. It is an incredible workhorse, and doesn't get the credit it deserves. I'm often amused by developers that revile PHP, while going on to use another language, which suffers from a similar set of problems to PHP, oftentimes with poorer performance and more complex toolchains. PHP deserves a little more love, imo.

PHP has its special place for every developer that works on web. And it got all recent improvements thanks to all those criticisms. I think every popular language receives criticism and that's just fine. I'm happy that the language is evolving and there is a strong ecosystem with quality libraries and developers unlike what it used to be 5 years ago. I've been doing several languages (Scala, and Typescript. also Go r…

A static (serverless) website hosted on something like github pages or S3 paired with some API calls to an nodejs application running on a single core vm instance, both behind cloudflare, is also extremely cheap and should be able to easily beat php in terms of performance/costs ratio.

Re: Everything you need (and don't need) to know about PHP's type system

#59
post #57

Earlier quoted context omitted.

PHP has its special place for every developer that works on web. And it got all recent improvements thanks to all those criticisms. I think every popular language receives criticism and that's just fine. I'm happy that the language is evolving and there is a strong ecosystem with quality libraries and developers unlike what it used to be 5 years ago. I've been doing several languages (Scala, and Typescript. also Go r…

A static (serverless) website hosted on something like github pages or S3 paired with some API calls to an nodejs application running on a single core vm instance, both behind cloudflare, is also extremely cheap and should be able to easily beat php in terms of performance/costs ratio.

Yes you are right. There are a few really good frameworks like Gatsby and NEXT for building static websites since last few years. JS/NodeJS/TS development cost is also quite moderate and good.

JS ecosystem still lacks good reliable vendors and that holds some companies away from it. Surprisingly it's holding up really well even though must of the packages are maintained by individual contributors.

Re: Everything you need (and don't need) to know about PHP's type system

#60
post #14
post #3

PHP rocks in 2020. Couple it with laravel or something and you get a solid platform to build a monolith-type web app with really fast performance.

Isn’t Laravel the worst performing framework out there right now, like even Rails looks like a viable option in terms of speed?

The last time I looked at a rails-clone framework speed comparison chart rails was the slowest, with some Sinatra clones being the fastest.
Post reply on HN