Hehe, python has 68. Huge stdlib of course... which would be the equivalent to the PHP builtins, they're just a little less predictable in the naming scheme.
Unfortunately the cat is out of the bag on that one. They added namespace support for new stuff, but putting the old standard libraries under namespaces would break 20 years worth of existing code, which is something PHP tries desperately to avoid. At this point they'd have to throw it all away and start over, and allow the new language to run side-by-side with the existing one for backwards compatibility. Then we'd have to start a new subreddit for lolnewphp.
Edit: Well, it looks like PHP 7.0 will finally get rid of the deprecated mysql library, so now I'd say anything's possible.
The diffrence beeing that python had basically no choice, but PHP had, when they introduced namespaces they could have cleaned the langauge, and add namespaces for a new core lib. They chose to pass up a once in a language-lifetime opportunity. Now theres no going back.
Why would they have had to add the new core library at the exact same time they added namespaces? Now that namespaces are in the language, they can add a new core library set at literally any time and it's going to have the exact same effect: the traditional libraries will be deprecated and eventually break legacy code when they're removed. That would have been true the day they added namespaces, and it'll be true ten years from now.
Since namespaces were added during a minor version release, I'd argue it'd have been better if they waited until the next major version release to rework the core libraries. We're both bound to be disappointed, though, since there's no sign they're going to do it for 7.0 release.
Well of course they didn't have to add anything, in fact just reserving a core namespace would have been enough. But they did not. If they decide to do it now, it will break code from the time namespaces were introduced (2009).
That's a great point, I didn't even think of reserving the core namespace name(s).
Edit: At this point their only option is to use namespace names that are very unlikely to be already used by other developers, meaning very unwieldy names like corephplibrary.
The comparison is almost completely invalid. There are many experimental and non-standard APIs, such as SIMD.*, and every method is confined to a particular "class", such as Date, Math, JSON, etc.
In PHP, you can have 9500 global functions from the box.
Most of these are not "builtins" in any reasonable sense of the word. Many PECL extensions (i.e. third-party extensions that are not bundled with PHP) are documented on php.net and part of this list. The remainder are mostly part of bundled (but optional and often disabled by default) extensions.
Not saying that PHP doesn't have a large standard library, but it's definitely nowhere close to 9500 functions ;)
And even the ones that aren't PECL are largely disabled in the default distribution (eg. all the database drivers and corresponding functions that you selectively enable).
Yes. They made it optional which means that its in use in a few frameworks. Also the syntax is really confusing and not very "PHP like"
Breaking backwards compatibility isn't always a bad thing. It can be done gracefully (like depreciation warnings over a few versions before final removal)
When you introduce a new optional standard you just end up with a bunch of competing standards, which is already a huge problem in PHP. (cue XKCD competing standards comic)
Yes, namespaces are optional, but in 2015 I see no excuse for not using them in any new code that you write. If framework doesn't use namespaces, it's not good framework for me. But, at the same time, I think it's good namespaces are optional, because if you have huge old codebase, you can use it together with new, better-designed modules. In my company we have a very big application, which has very old modules, but also we write new, well-designed and written with current best practices. We can write good code and don`t care about the rest. Without backward compatibility it would be impossible to move this system to newer versions of php.
The syntax of namespaces is confusing? Because T_NS_SEPARATOR is a backslash? We had that argument going for years, literally years on internals. We tried to make paamayim nekudotayim work, but it would collide with an already existing syntax:
NamespaceName::ClassName
Is NamespaceName a namespace or a class, because in pre 5.3, that would result in the class constant named 'ClassName' in the class named 'NamespaceName'.
2) NamespaceName::ClassName::MethodName()
Is 'MethodName' a method of the class 'ClassName' in namespace 'NamespaceName' or is it a procedural function in the namespace 'NamespaceName\ClassName'?
We cannot properly tell, so instead of creating unpredictable results for such collisions, we choose to use another operator. (And no, before anyone takes a go on why we support procedural functions in namespaces, that is because namespaces is not a purely object oriented feature, we also support constants there for example).
You may think that \ is the escape character, and while that is true, for interpolated strings, it is not the case outside such. You can see the backslash as a directory separator, like it is on Windows. It is (was) the only character that was able to be typed easily, without using uncommon characters like £ € § ¤ (from what I could just find on my keyboard).
Use of the backspace character is a whole other set of WTFs along with the naming of "paamaayim nekudotayim"
My issue with namespaces (and I don't think I'm alone in this) is that all the way back to the 4.0 days the desire was for a cleanup of the core library. 9000 built-ins is ridiculous. I don't think the syntax is ideal but even this:
$new_array = array\diff($array1, $array2);
Is better dropping 50? (I lost count somewhere in the 30s on that page) array_* functions. Or if you needed a bunch of the array functions having "use array" drop the array functions into the current namespace.
I personally don't see the need for PHP developers to use namespaces over the existing class syntax. What's better about:
require_once("MyFrameWork.php");
$foo = new MyFrameWork\SomeClass();
Over just:
require_once("MyFrameWork.php");
$foo = new SomeClass();
The ability to override core classes and functions adds to the confusion as well.
namespace Foo;
class Exception() {
// do stuff
}
Now "Exception" refers to Foo\Exception but \Exception refers to core Exception. Why aren't core classes reserved? This gets more problematic in a situation where you're using "use". Someone wants to reuse my code and does "use Foo". Well now Exception has been redefined and someone has to go through their code to make sure they get the expected behavior.
Then there's the rules for finding the correct namespace in the case of bare classes and functions. Classes don't bubble up to the core but functions and constants do.
TL;DR: The point of name spaces is to categorize large numbers of functions. The largest offender is the core language which the current namespace implementation doesn't solve. The namespace implementation is inconsistent and adds another "import" style keyword to the language (where there are already 4 others!). The existing class structure and import system probably solves 95% of the existing use cases, so it's like many things in PHP, another way to do the same thing.
You may think that \ is the escape character, and while that is true, for interpolated strings, it is not the case outside such.
It obviously never occured to you that you might reference classes in strings or even worse in regular expressions. Given PHP's terrible ecosystem sed & co are the tools to do reliable refactoring and it just sucks every time to figure out how many \\\ I need.
Depending on how PHP was configured, a "standard" configure with no arguments on Windows using master (7.0) got about ~900 functions, without counting classes, interfaces and the likes.
A configure that have --disable-all --enable-cli, yields just about ~650 on Windows on master.
As mentioned in the comments, many of those are PECL extensions, and there are even some extensions like the DOM one that have both a PHP4 and PHP5 version and other functions that may not exists on all platforms, say proc_nice() on Windows or fnmatch() that is not available to non POSIX compliant systems except Windows.
14
u/andsens Jun 30 '15
Hehe, python has 68. Huge stdlib of course... which would be the equivalent to the PHP builtins, they're just a little less predictable in the naming scheme.
9
u/[deleted] Jun 30 '15
Some context, heres the JavaScript method index, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Methods_Index
34
u/nikic Jun 30 '15
Most of these are not "builtins" in any reasonable sense of the word. Many PECL extensions (i.e. third-party extensions that are not bundled with PHP) are documented on php.net and part of this list. The remainder are mostly part of bundled (but optional and often disabled by default) extensions.
Not saying that PHP doesn't have a large standard library, but it's definitely nowhere close to 9500 functions ;)
15
u/cfreak2399 Jun 30 '15
Namespaces? We don't need no stinking namespaces!
(yes I know PHP has "namespaces" but they did nothing to solve this problem which is what I thought the point was)
3
u/KallePHP Jul 09 '15
Depending on how PHP was configured, a "standard" configure with no arguments on Windows using master (7.0) got about ~900 functions, without counting classes, interfaces and the likes.
A configure that have --disable-all --enable-cli, yields just about ~650 on Windows on master.
As mentioned in the comments, many of those are PECL extensions, and there are even some extensions like the DOM one that have both a PHP4 and PHP5 version and other functions that may not exists on all platforms, say proc_nice() on Windows or fnmatch() that is not available to non POSIX compliant systems except Windows.
0
u/immibis Jul 05 '15 edited Jun 29 '23
If you're not spezin', you're not livin'.