Skip to content

Comment on What PHP Needsparent

Comments

> That might be just how I feel though.

No, I got the exact same impression. PHP is a language that takes away all the fun in programming for me.

And for someone programming-dumb like me it is the opposite. PHP enables me to quickly create websites and it is fun to do that.

I believe that this (ideas thrown around) is the truth, Rasmus Lerdorf comes across as a very pragmatic developer who's more interested in getting things done rather than being an architectural perfectionist.

But I'm not saying that in a derogative way. Coming from C#, Java and Ruby, I feel that PHP is kinda fun and refreshing. The object model is quite simple, and I love the $$ (variable variable) syntax, and how 'reflection' is way simpler than any other language. It's language for hackers.

"and how 'reflection' is way simpler than any other language"

I've never heard this claim made, and it has been a long time since I've actually used PHP in any meaningful way. Care to expand on this?

I'd be interested in a comparison to Python's support for reflection, which is simple and pervasive.

Just a disclaimer: I have zero work experience with PHP, just personal experience.

I put it inside quotation marks because it's not the true-reflection API per se, but I believe it was a mistake to even bring that up, and to generalize by saying it's easier than 'any other language'.

I was talking about old methods like get_class_methods, get_class_vars, get_object_vars and $$.

Here's an example which I used to answer another user above:

    $a = new A;
    foreach(get_object_vars($a) as $name => $var) {
      $a->$name = $a->$name + 1;
      echo $a->$name . "\n";
    }
By no means those things are absolutely "beautiful", but they get the job done and they're one-liners, hence, simple. They're simpler than Java and C#, languages which I worked for years. Also they're easier than Ruby sometimes.

To be honest, I just think it is fun. I can make things in PHP with five lines that would take me multiple pages in C#, but the opposite is true too, for other areas.

Reflection is simpler than any other language?

Here's an example from the PHP docs on the reflection API, in the comments:

  $a = new A();
  $reflector = new ReflectionClass('A');
  $properties = $reflector->getProperties();

  $i =1;

  foreach($properties as $property)
  {
    //Populating properties
    $a->{$property->getName()}=$i;
    //Invoking the method to print what was populated
    $a->{"echo".ucfirst($property->getName())}()."\n";
   
    $i++;
  }
You have to instantiate another object to inspect the first one! Here's Python, just as an example. (Most languages would be simpler. As with most things, it would be difficult to find a language where reflection was more clunky than in PHP.)
  a = A()
  for i, key in enumerate(a.__dict__):
    setattr(a, key, i)
    f = getattr(a, 'echo'+key.capitalize())
    print(f())

On another thread somewhere, someone once suggested that PHP is actually Blub -- a fictional language whose immense and inherent superiority cannot be explained, only experienced.

As a long time PHP dev, let me assure anyone who thinks that this is the case: in no way is PHP Blub.

> You have to instantiate another object to inspect the first one! Here's Python, just as an example. (Most languages would be simpler. As with most things, it would be difficult to find a language where reflection was more clunky than in PHP.)

I was about to agree with you then I remembered the work put into the Newspeak language (which I was once hopeful would replace Smalltalk as my favorite).

In Newspeak, reflection is always indirect because of security reasons.

You ask the system to give you a Mirror, and that mirror can reflect against an object you have access to. Security name-spacing and authorization is encapsulated into the mirror, so you can say:

- Give unauthorized users access to your VM with live objects, but they can't change anything because their Mirrors only allow for inspection.

- Make changes to a live object indirect. So if you add an attribute to object live object X, the system will go out of band---say to the database or announce to remote systems---that the change occurred and further action may be needed. That's great for access audits.

In that respect, PHPs reflection class isn't too bad. Though I think its a bit underpowered.

"Blub" isn't a fictional superior language; it's a fictional average language probably imagined to be similar to Java. It was, so far as I know first described here: http://www.paulgraham.com/avg.html

PHP is Blub in the sense that it is not the most powerful language[0], and some of its users decline to recognize that other languages have advantages over it. From my perspective, there can be no perfect language; certain characteristics are more optimal in some situations, less so in others and mutually exclusive. Type safety and certain kinds of dynamic behavior are an example.

[0] I'm not claiming here that any specific language is the most powerful, just that PHP certainly isn't.

Yes you're right, I got the Blub parable wrong.

What I mean is that the commenter believed that all other languages are Blub to PHP, as in, PHP is higher on the spectrum of powerful languages and other language users did not realize that they were looking up when they looked at PHP.

The naming of ReflectionClass is enough to make me want to cry.

I know naming things in CS is hard, but if you're having to put the word "Class" in your class names to make your code readable then you've got real problems.

It's a Reflection of a Class. Similarly there is ReflectionObject, ReflectionMethod, ReflectionParameter, and ReflectionProperty.

I suppose that's fair enough. It's not exactly ideal though is it, but PHP's lacklustre naming conventions are hardly ground that needs to be trodden again.

That's why I put the word reflection inside quotes, I wasn't talking about the proper reflection API, but about functions like get_class_methods, and stuff like $$.

Here's an example similar to yours:

    $a = new A;
    foreach(get_object_vars($a) as $name => $var) {
      $a->$name = $a->$name + 1;
      echo $a->$name . "\n";
    }
Also I'm a newbie when it comes to PHP, I never worked with, and I've been programming for almost a decade. Sorry if I didn't make it clear. I just wanted to make a positive comment on the cool aspects of a language which is mostly bashed around these parts.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.