3 ms·
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
by jessedhillon 15y ago
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.
- Zak 15y ago"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 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.
- jessedhillon 15y agoYes 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.
- deleted 15y ago[deleted]
- true_religion 15y ago> 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.
- JonnieCache 15y agoThe 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.
- wvenable 15y agoIt's a Reflection of a Class. Similarly there is ReflectionObject, ReflectionMethod, ReflectionParameter, and ReflectionProperty.
- JonnieCache 15y agoI 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.
- glassx 15y agoThat'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.