isset($foo, 2)
Returns a parse error, even though isset looks like a function call and you're just passing it an extra parameter. sizeof(int, 2)
will return a parse error, because sizeof is an unary operator despite looking like a function (the parentheses are mandatory when using it with a type name). In PHP, isset is implemented as a "language construct", essentially like another operator.No, I got the exact same impression. PHP is a language that takes away all the fun in programming for me.
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.
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.
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.
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.
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.
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.
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.
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.
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.