It really pains me that there are so many utter misunderstandings about modern PHP from people that used it years ago and have no clue about the current landscape, even in this thread. Yes, the PHP core api is still a bit messy due to backward compatibility, but everything else it pretty awesome really. Please take a closer look if you haven't touched PHP in years, before you talk it down.
> This is profoundly ridiculous. It's the equivalent of saying Home Depot shouldn't sell power tools because some do-it-yourselfer might saw his foot off. You're incorrectly characterizing the argument: it's actually like saying Home Depot shouldn't sell power tools without safety guards, fuses, emergency shutoffs, SawStop, etc. PHP has a number of misfeatures which make it extremely easy for all but the most vigilan…
Here's all you really need to know with PHP, ask a PHP developer how to test for equality. Maybe how you test for equality shouldn't depend on an ini file... just sayin... Perhaps testing for equality doesn't need a 3 page answer... in most languages it's a pretty straightforward answer... primitive types use == and objects use either isEquals or operator overloading.
What do you mean by depend on an ini file? I wasn't aware there was any settings for equality testing there..
> PHP's "closures" require you to write down the free variables in an enclosed function and tell PHP that they exist Can you give an example? I googled "closure conversion" but didn't understand.
In order to use variables from the scope that the anonymous function came from, you have to explicitly import them into the anonymous function with the use[1] block: public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk(…
Yeah, so? Sounds even better, as you can only declare what you'll actually use from the enclosing scope.
> generators for simpler iteration, namespaces, and variadic functions and argument unpacking. With PHP 5.4, traits were introduced (a la Scala or Perl) to allow code reuse in single inheritance languages, as well as closures, which allow you to code PHP in a functional style. "The new Java" It's good to see PHP maturing. However sometimes the most important features of a language are the features you don't add. I th…
I realize the PHP API is a mess compared to highly curated and standardized offerings like Java, but growing up with it, it was very like C standard lib and other things of the time. I could often even guess functions without looking them up even though they had weird names and abbreviations.
...and you could guess wrong (because consistency is for suckers) and have things blow up in entertaining ways.
In order to use variables from the scope that the anonymous function came from, you have to explicitly import them into the anonymous function with the use[1] block: public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk(…
Yeah, so? Sounds even better, as you can only declare what you'll actually use from the enclosing scope. Besides, doesn't Python work the same way?
Wrong. You only need to use the free variables of the closure, you don't need to copy the entire environment from the function you're defining your closure in. By the definition of a free variable you actually use it, otherwise it wouldn't be a free variable of the function! Python definitely does not work the same way, but in Python "x = 3" can inadvertently shadow (make a local name) something defined in the outer scope, but that's a completely different problem.
In order to use variables from the scope that the anonymous function came from, you have to explicitly import them into the anonymous function with the use[1] block: public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk(…
Yeah, so? Sounds even better, as you can only declare what you'll actually use from the enclosing scope. Besides, doesn't Python work the same way?
In modern versions of Python you can declare "nonlocal x", but that's only necessary if you want to assign to x. Since assignment in Python creates a local variable by default, there needs to be a way to tell the byte compiler not to shadow the nonlocal variable.
If you don't assign to x, the compiler will do the right thing without help.
> PHP's "closures" require you to write down the free variables in an enclosed function and tell PHP that they exist Can you give an example? I googled "closure conversion" but didn't understand.
It is an algorithm used in compiling languages like Scheme where you calculate the set of free variables in a function and then create a closure (a function pointer and an environment, which is basically a way to lookup free variables). PHP requires you to do this by hand with the "use" keyword, e.g. use($a, $b, $c) where $a, $b, and $c are free variables (that is they are not bound in the current scope). I see no re…
Last I checked, PHP was open source. Why don't you go and do this for the "lazy devs" if it bugs you so much.