3 ms·
I'm a programming language geek, a code lover and a syntax fanatic. And there are a ton of things I love about php: class Animal // yeah! real classes!
by xyby 12y ago
I'm a programming language geek, a code lover and a syntax fanatic. And there are a ton of things I love about php:
class Animal // yeah! real classes!
{
function hello($name="nobody") // nice default variables
{
echo "hi $name"; // sexy variable expansion!
}
}
class Dog extends Animal // logical syntax :)
{
...
}
function f()
{
$x=123; // variables are local by default and need no initialisation
}
I could go on for a long time, listing all the language constructs I like in PHP. All other languages I tried have quirks that annoy me.
- k__ 12y agoYou're joking, right?!
- califield 12y ago`f` in your example is a global function that cannot be redefined.
- xyby 12y agoWhich is a good thing. When you have ... function f($x) { return $x*$x; } ...in your code, you know that you can always call f() to get the square of something. Hallelujah. Functions as they are meant to be.
- imaginenore 12y agoDon't like global functions? Don't use them. It's really that simple.
- chc 12y agoRemarking on the fact that variables are local by default makes me wonder exactly how many programming languages you're familiar with. That's an extremely low bar, not really something to love.
- arenaninja 12y agoConversely, there's a long list of languages where variables would not be local by default
- chc 12y agoInsofar as there is a ridiculously long list of languages nobody has ever heard of, I guess this is true. But it's not true if you're comparing it to languages that are actually used in modern programming. Even if you're considering languages as obscure as, say, Standard ML, you'll still not find many that have a default global scope for variables inside functions. There's pretty much JavaScript, Perl, Fortran and Cobol. I can't think of any others that are even worth mentioning nowadays.
- srgpqt 12y agoLua. At least there is Moonscript to save it...
- xyby 12y agoLow bar or not, if you do anything web-facing, there is no way around javascript. And you will do a lot of stuff in a language that makes variables global by default.
- Shish2k 12y agoA car metaphor for this conversation: "I love my skoda! It has four wheels, and a roof, and a passenger seat!" "That's a pretty low bar for things to love about a car. Pretty much all other cars also have those features, and they do them better." "Yeah, well I also ride a bicycle, and my bicycle doesn't have those features."
- tiglionabbit 12y agoI'm a programming language geek, a code lover and a syntax fanatic. And there are a ton of things I love about coffee-script: class Animal # yeah! real classes! hello: (name="nobody") -> # nice default variables console.log "hi #{name}" # sexy variable expansion! class Dog extends Animal # logical syntax :) ... f = -> x=123 # variables are local by default and need no initialisation
- xyby 12y agoCoffeeScript is interesting. So far I did not experiment with it too much. So I'm not sure if I would like to do work in it. On one hand, it looks nicely terse. On the other hand it looks a bit cryptic. The thing that stops me from trying it for a project is not the language, but the available environments. Having an additional compliation step adds complexity to the system and the workflow. One thing I cannot resist to say is that to me "hello $name" is sexier then "hello #{name}" :)
- tiglionabbit 12y agoWell sure, but you can put arbitrary expressions in the #{} syntax. PHP has a similar syntax for that. I just run `coffee -wc .` on my source directory and pretend I'm using JavaScript everywhere else.
- Shish2k 12y ago> nice default variables If you think that's nice, I'm about to show you something which will BLOW YOUR MIND :D >>> def hello(name="anybody", age=10): ... print "hello %(name)s, aged %(age)d" % locals() ... >>> hello(age=42) "hello anybody, aged 42" But wait, there's more! > yeah! real classes! You think PHP's classes are real? Just wait until shit gets REAL real when you subclass a primitive! >>> class ExtremeInt(int): ... def __new__(cls, val): ... return super(ExtremeInt, cls).__new__(cls, val) ... def __add__(self, other): ... return (self * other) # Multiplication is EXTREME! addition ... >>> >>> x = ExtremeInt(42) >>> print x + 2 84 Dare I stop there? I dare not! > sexy variable expansion! You think formatting strings based on local variables is sexy? Sure, we can do that: >>> name = "bob" >>> print "hello %(name)s" % locals() hello bob But why not format a floating point number to a set number of decimal places based on a dictionary? >>> things_to_template = {"pi": 3.14159} >>> print "My pi is %(pi).2f" % things_to_template My pi is 3.14 What about formatting based on an object's attributes? >>> class Animal(): # HOLY SHIT REAL CLASSES!!!! ... def __init__(self, name): ... self.name = name ... >>> my_ferret = Animal("Mr Awesome") >>> print "My ferret's name is %(name)s" % my_ferret.__dict__ My ferret's name is Mr Awesome Is that the end? It's 4am here and I'm barely conscious, but no! > logical syntax [for single inheritance] :) Ready to have your mind blown FOR A SECOND TIME?!?!? >>> class Animal: # SUCH CLASS. ... def __init__(self, spec): ... self.spec = spec ... >>> class Awesome: # SO REAL. WOW. ... def __init__(self, awesomeness): ... self.awesomeness = awesomeness ... >>> class Ferret(Animal, Awesome): # Logical syntax for MULTIPLE INHERITANCE OMGOMGOMG ... def __init__(self, awesomeness): ... Animal.__init__(self, "Ferret") ... Awesome.__init__(self, awesomeness) ... >>> mr_awesome = Ferret(9001) >>> print "Mr Awesome is a %(spec)s and his awesomeness is %(awesomeness)s" % mr_awesome.__dict__ Mr Awesome is a Ferret and his awesomeness is 9001 And that final part? > Variables are local by default ... ... Actually I can't think of a way to improve on that. It's like saying "I love this language for being turing complete". Sure, that's a good thing. The best thing, perhaps. But you sound kind of weird when you list it as a particular high-point of a language...