News for hackers. Stuff that matters.
Re:What I Like About PHP by Shish2k on Tuesday December 09, 2014 @10:23PM (#8727516)
> 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...

There are no comments on this comment yet.