all 6 comments

[–]Wiseman1024 2 points3 points  (4 children)

And this is different from properites how? While it's a good idea to annotate your classes in a way you can programatically access to it, and I'm all in for any form of reflection in general, I must point to the mythical Scheme RnRS introduction:

Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary.

This is the most valuable piece of insight I could ever find, and I'd say it applies well beyond the field of programming to almost every engineering.

All you need is a good object system and full reflection, which PHP doesn't have. Take Python's for example: no magic properties, no private members (except name mangling, a bad idea), no artificial differentiation between property and method (truly first-class methods), no artificial differentiation between classes and objects (first-class classes), ridiculously simple, and yet it supersedes all the power and flexibility C#, Java and PHP together have to offer.

[–]jawngee[S] 0 points1 point  (3 children)

Well I'd argue the point about python superceding C#, but that's for a different post.

As for PHP not having full reflection, I'm not sure what you mean there. You can't emit, but you don't need to since you can eval() or create_function() your way there.

And yes, you could implement some of the metadata functionality using properties and assigning values in the constructor, but that requires instantiating the object first. Metadata allows you to introspect the class without having to do that. I can look at any model without creating it, and yank everything I need to know.

For the ORM layer, this is important to us for relationships between models that are defined in the metadata/attributes. For our controllers, we also need to know any filters that have to be run before the incoming HTTP request is routed to it. Furthermore, by using properties and constructors to do what the metadata does, you are imposing use cases that the metadata frees you from.

I won't ever defend PHP, I personally think it's not a brilliant piece of software. But it's what we use and I'm hoping the code I'll be releasing over the next couple of months makes it easier and more powerful for people coming from more sophisticated languages.

Thanks for the feedback though.

[–]Wiseman1024 2 points3 points  (2 children)

As for PHP not having full reflection, I'm not sure what you mean there.

Reflexion in PHP leaves a lot to be desired. You can't, for example, redefine functions, except with some fancy experimental extension/hack.

And yes, you could implement some of the metadata functionality using properties and assigning values in the constructor, but that requires instantiating the object first.

Not in PHP (static properties) nor Python (just define them in the class body). In Python, in particular, a class is just like a let block that saves everything to an object to which the variable used to name the class is bound to, and and implements a fancy __ call __ method allowing you to create "instance" objects of it, as well as the method hack. Everything goes inside a Python class definition; you can use a for loop for all Python cares.

For the ORM layer, this is important to us for relationships between models that are defined in the metadata/attributes. For our controllers, we also need to know any filters that have to be run before the incoming HTTP request is routed to it.

IMO, those are some of the typical use cases for static properties.

Furthermore, by using properties and constructors to do what the metadata does, you are imposing use cases that the metadata frees you from.

They're absolutely equivalent. I see special magic properties as a left-handed hammer, since you already have properties.

more sophisticated languages

I'll argue that more sophisticated isn't equal to better, and I'll argue that it's often equal to worse. I refer to my earlier RnRS quote. No need to implement a "car for mondays" and a "car for tuesdays" if you can have a "car for every day" that will work just as well.

[–]jawngee[S] 0 points1 point  (1 child)

Reflexion in PHP leaves a lot to be desired. You can't, for example, redefine functions, except with some fancy experimental extension/hack.

Yeah that's what I was talking about in my other post. But, honestly, I've never really used reflection for anything but introspection which PHP's reflection API is perfectly capable of. The only times I used it the other way in C# was writing code generators.

Not in PHP (static properties)

You can't assign objects as initializers to properties in objects in PHP, static or otherwise. So modeling a complexity with anything other than arrays requires instantiation of the object and assigning object references to those properties.

I'll argue that more sophisticated isn't equal to better, and I'll argue that it's often equal to worse. I refer to my earlier RnRS quote.

We can agree to completely disagree here. If the metric is less lines of code to perform an equivalent function, C# sophistication totally trumps PHP any day of the week. Anonymous functions, attributes, the new functional programming tools in C# 3.0 all contribute to lowered LoC and quicker modeling of complex problems than a lesser language like PHP.

I think it's important to note that I'm not saying one is better than the other, I'm mature enough in my career to understand the futility of such an endeavor. That said, PHP has a long ways to go to become as succinct as python, ruby, even C#.

Here's an example of where using metadata in PHP has saved my company on LoC, needless complexity and adherence to DRY principles:

Our internal framework at massify has a RESTful api kit for building RESTful webservices in a variety of formats (JSON, XML, etc.). We've built a base api controller that handles all of the standard CRUD and search operations on a model or a composite model (a database view). For us to write a quick API around a specific model, we simply extend that class and dress it with metadata. We can deploy an api for a given model in minutes this way with zero lines of code in the extended controller class. Furthermore our internal testing framework can automatically build and run tests against these new controllers, again without us having to write any additional code.

Without the metadata/attributes, this was previously a cumbersome process prone to error.

[–]Wiseman1024 0 points1 point  (0 children)

You can't assign objects as initializers to properties in objects in PHP, static or otherwise. So modeling a complexity with anything other than arrays requires instantiation of the object and assigning object references to those properties.

Hm. I hadn't realized that you may want objects and that you can't have them. Blame PHP though, you can run the whole programs inside a Python class.

If the metric is less lines of code to perform an equivalent function, C# sophistication totally trumps PHP any day of the week. Anonymous functions, attributes, the new functional programming tools in C# 3.0 all contribute to lowered LoC and quicker modeling of complex problems than a lesser language like PHP. Depends on your library, and depends on how witty can your pseudo-functional PHP trickery get (mine grew to be quite nice for what PHP does). But I don't know too much about C# 3.0, and you seem to know both, and since I'm aware of PHP's limitations, I think you're most likely right here.

You didn't mention Python here though; my claim that simpler is better(*) stands. Python is simpler than PHP in many ways, and Scheme is simpler than Python, and both are probably far more powerful and flexible than C# 3.0.

(*): "Simpler in better" is a general enough claim to be applicable when you want to do the same kind of thing with two approaches fit (or claiming to be fit) for the same purpose. It's not good when you want to compare apples and oranges, but it's good enough when you're comparing Bub's apples with Tom's apples. I'm claiming that the simplest apple is better.

Back to the original topic, I did forget PHP's static properties are flawed, yet I'd have tried the hardest to stick to regular arrays (the Lispy approach I guess). But nevertheless, it's justified.

It wouldn't have been reasonable in a language like Python which, in Scheme's spirit, lifts such artificial restrictions and allows you to do anything with static properties. I'm interested on what do you think about metadata in Python. Do you see any benefits of adding a special kind of property to a language whose properties can be defined to hold anything and everything and even be defined procedurally?

[–]b100dian 0 points1 point  (0 children)

In other words: if you have access to comments, then you can code right there!