Hacker News new | ask | show | jobs
by adaml_623 5482 days ago
Good to see. It's about time PHP got forked even if experimentally.

It seems to me that you could fix a lot of the problems with PHP by breaking backwards compatibility.

3 comments

Many of the problems and annoyances with PHP could be fixed while keeping backwards compatibility.

Just a short list I can think of now:

- Short array syntax. Completely optional but would clean up the language quite a bit. This has been proposed several times and shot down each for no real good reason.

- Promote complex primitives into objects. With the proper interfaces it wouldn't break any old code. Once this is done, deprecate the myriad of str* and array* functions in favor of the object methods. This also cleans up the "what order do i pass in the args" problem that so many people cite.

- Named parameters. Gets rid of passing in default params or an array blob for functions/methods that take lots of optional params. It also opens up the way for DSLs.

- Replace PEAR with something better. The quality of PEAR modules is really low and PEAR always seems to be broken one way or another.

- Add a list and hash type. PHP's array is very slow for some cases, and sometimes you need to enforce the datatype you're working with.

- Use exceptions for errors. Could be a runtime flag to prevent breaking things. It's incredibly easy to write bad code because PHP make it easy to ignore problems. Things like functions that connect to the db or trying to retrieve data from a stream don't throw exceptions on error.

Well to be honest if you deprecate the str* and array* functions you are pretty much breaking backwards compatibility.

I'm not saying it wouldn't be a good thing but those functions are one of the defining features of PHP imho

Deprecation is not the same thing as removal.
Regarding your first point: it seems short syntax for arrays may well make it into PHP 5.4.

http://www.reddit.com/r/PHP/comments/hw8da/php_fork_based_on...

PHP already provides a mechanism to replace errors with exceptions (set_error_handler() and the ErrorException exception type). I do this on all my projects.
I'm aware of that method. It's not an ideal approach IMO.

Specific errors should throw specific exceptions. That way I can handle the ones I know how to handle and let the others bubble up and either completely break the app to be logged and fixed later or get handled correctly somewhere else. With ErrorException, you have to look at the contents of the error message to see if it's an error you can handle or need to rethrow.

If something in PHP triggers an error, there is very little to recover from. It's either a developer error or an unrecoverable problem (a file expected to exist was not found, network is down, etc). In any of my projects, I've never needed to recover from PHP ErrorException.
What would a sane version of PHP look like? What are the good parts of PHP besides easy setup?
The good parts of PHP, just off the top of my head: Extremely forgiving syntax which makes it quick and easy to bash together something that just works, implicit type conversion, a vast array of built-in functions, readability, near-ubiquity, generally very strong documentation with a healthy focus on example code, and the way it interpolates with static HTML, which I'm sure there's a word for but I can't remember offhand.
What I see as the big strength is how easy it is to set up mod_php - possibly in no small part due to how most Linux distributions have solid defaults for mod_php5 and keep it up to date. The things you listed I'm not so sure about.

I don't think it's the syntax that's forgiving as much as the type coercion. JavaScript is actually more forgiving syntax wise, for example - PHP throws a fatal error when a semicolon is missing at the end of a line, JS interpreters fill it in. Unlike PHP, though it will throw a ReferenceError and die upon referencing any undefined value. PHP just coerces that to something falsey... they'll both die trying to call undefined functions or methods.

I don't see how PHP is not more readable than Python or Ruby, either. There's really nothing about the language itself that makes it readable or better for beginners.

The docs are definitely good, I agree. I think they need to do something about the outdated and not-so-well curated comments on each entry though.

Near ubiquity is good, but it also makes a lot of good PHP resources get lost in horrible noise. WordPress, for instance - if you search to solve anything, you find scores of outdated tutorials offering solutions to problems which have been solved by the official branch for years. Then, you find more blogs blog-spamming the incorrect solution... so it's actually easier to search for and find Rails or Django help, in my experience. A large percentage of the PHP code out there is notably terrible, for whatever reasons.

The 'interpolation' would be called templating. You can do this in most languages (erb for Ruby, for example) but it's not always considered a good idea to give the template language logic capabilities. It happens that this is the default for how PHP and Apache work together, and I think the straightforward concepts of files, directories, scripts it's easier for beginners to understand and work with than framework style URL routing.

The large software eco-system centered around tools consumers use such as WordPress or joomla has helped PHP grow.

Set up is easy, but the majority of PHP users probably never set up their own Linux boxes with Apache anyhow. If they wanted to, though it would be easier than say, Django since last I checked Ubuntu was still providing Django .96 packages.

The ease of setup comes from every shared host offering PHP and MySQL. If they all offered Python, Django and Postgres support, with cpanel configs and PhpPgAdmin or the like, people would magically find Django really easy to set up. That's just how it goes when you have lots of market share.

Date and time handling with date() and strtotime(), magic methods, simple and practical array handling, proximity to the HTTP so you can solve problems the way you want instead of the way someone tells you you should.
One good point in PHP is the hello world code is just "hello, world". Not print("hello, world") neither <?php echo "hello, world"?>.

Simply type in hello world in a file, save it and run it as PHP script. It will print "hello world". And that was the start of the good things.

or just moving on.

php itself does break backwards compatibility, and it doesnt really help.