6 ms·
Fun with PHP: $a = 'b'; $b = 'a'; echo $a."\n"; >>> b echo $$a."\n"; >>> a echo $$$a."\n"; >>> b Yaaa $obj = new StdClass
by Arnor 12y ago
Fun with PHP:
$a = 'b';
$b = 'a';
echo $a."\n";
>>> b
echo $$a."\n";
>>> a
echo $$$a."\n";
>>> b
Yaaa
$obj = new StdClass();
$obj->x = 1;
function x_plus_one($obj) {
$obj->x++;
}
x_plus_one($obj);
var_dump($obj);
>>> object(stdClass)#1 (1) {
>>> ["x"]=>
>>> int(2)
>>> }
Huh? A local change to the object changed the object outside the local scope? It must have been passed by reference...
function null_the_object($obj) {
$obj = null;
}
null_the_object($obj);
var_dump($obj);
>>> object(stdClass)#1 (1) {
>>> ["x"]=>
>>> int(2)
>>> }
Huh? Before when I edited the object within the function, the object I passed in was edited, what gives?
We don't actually pass objects to reference in PHP, we pass them by "object reference." But don't worry, you can still null objects that get passed to you:
function null_the_object_2(&$obj) {
$obj = null;
}
null_the_object_2($obj);
var_dump($obj);
>>> NULL
Sure it's not as bad as it used to be. Yes, a disciplined developer can make good software with it, but it's still a pretty quirky.
- opendais 12y agoTo be fair, all languages are "a bit quirky". It gets worse when you start including things that are in the PHP stdlib but aren't in other languages by default. [e.g. When they basically re-implement how php-fpm handles requests and thread safety]
- Arnor 12y ago>>> To be fair, all languages are "a bit quirky". Agreed. These are just a few quirks in PHP I find amusing.
- opendais 12y agoThey are amusing indeed. :)
- Zancarius 12y agoI'm sure you've already seen this (it's been around a while), but if not, you'll find it amusing: http://www.phpwtf.org/ http://www.phpwtf.org/ Last I checked, it wasn't quite as good as wtfjs, but it looks like quite a few new surprises have been added rather recently. Edit: Correction, there's only one new entry as of this year, which is a shame. :)
- wvenable 12y agoThe first two I read aren't even WTF the worthy and are perfectly reasonable. Even in the classic fractal of bad design article, only 1/3 of it is actually valid.
- Zancarius 12y agoI half agree, but I'd like to point out that the likely reason the curator of this site thinks of these as "WTFs" has more to do with PHP's (lack of?) consistency and it's relatively bizarre behavior when compared with saner languages. The other thing to consider (looking at it again, particularly at the second example) is that there are some surprising side effects that can snag newcomers, depending on their background and whatever other languages they know. Perhaps one of the drawbacks with programming PHP is that you become so desensitized to weirdly inconsistent/surprising behavior that there's almost nothing left to be surprised about.
- wvenable 12y agoIt's nit-picky to the point of ridiculousness. Take the first example, even the title "Objects with __toString are not strings." is already WTF worthy. Of course objects with a __toString method are not strings! It means they can be converted to strings when unambiguously used as strings. Which is exactly what happens! The whole entry is stupid. The author even complains that PHP will issue an error if __toString doesn't return a string! I mean really. Most other entries involve doing something bizarre with obviously undefined consequences and then marveling that PHP does it different than they expect. Or failing to understand PHP's basic rules and then again, on edge cases, marveling at weirdness of the perfectly logical result. The "PHP protected and an assault" entry just fails to comprehend how access modifiers actually work with the classes. There are real PHP WTF's but honestly they were all discovered 10 years ago. Probably 50% of all entries on the site are people misunderstanding how references work over and over and over.
- deleted 12y ago[deleted]
- artpar 12y agoJava willl have the same behaviour for function like x_plus_one. And function null_the_object_2 cannot be made in java.
- improv32 12y agoHonestly the reference passing quirks you mentioned behave exactly as I would have assumed, am I alone in this?
- mbreese 12y agoNope. I'm more of a Python/Java person and even I expected the observed behavior. Given that Python is my primary language, I'm pretty sure that I'm supposed to hate on PHP as much as I can, but really, this is stretching it.
- wvenable 12y agoThere is nothing unexpected by this behavior -- this works exactly the same as any language with the same features (C++, C#, etc). I don't know why you found this at all surprising; it's not at all quirky in this example.
- umanwizard 12y agoGot any counterexamples? What well-known OO languages don't work like this?
- DigitalSea 12y agoIf you're going to rag on a programming language on a site like Hacker News my advice would be to make sure you know what you are on about before you do. There isn't a programming language out there that doesn't have its quirks, look at Javascript, it has more quirks than you can poke a stick at, but that doesn't make it a bad language. In your examples I am not seeing the issue? Am I missing something here? Your examples work exactly as I would have expected them to, as they would in other similar languages like Java and C++. Lets stop the hate for the sake of hating PHP, it definitely has its quirks in relation to methods and order of parameters in particular, but things are getting better and they're not deal-breakers for getting things done. If you want to see how great things are in PHP, I would checkout the Laravel PHP framework. It is a very well built and robust framework that uses the best parts of PHP and abstracts some of the worse parts to make the experience of developing in PHP fun again.
- Pacabel 12y agoThe presence of some quirks isn't the issue. Of course every programming language has some. PHP and JavaScript, however, have unjustifiable quirks with some of their most basic functionality. We're talking about stuff as basic as their comparison operators being quite broken, for example. That's where the problem is. Furthermore, the fact that these issues have been around for well over a decade or more without being properly addressed just makes the situation even worse. Compared to other mainstream languages like C, C++, Java, C#, Python, Ruby and even Perl, both PHP and JavaScript are quite inferior in many critical ways. Perhaps things will improve over time, but we really haven't seen that happen so far. So let's not pretend that they're "good" just to be politically correct or to try to avoid offending anyone. PHP and JavaScript aren't good languages, and they won't really improve until their communities admit that there are some very serious issues, and then commit to getting them resolved properly and rapidly.
- Dewie 12y ago> The presence of some quirks isn't the issue. Of course every programming language has some. It is a very weird argument that often comes up when discussing programming languages - "Lang X is weird/stupid in some ways, but no language is perfect, so it's fine and there is no reason to call it out." So, because nothing is perfect (almost by definition), it apparently isn't a big deal that some things are weird/dumb/broken. And let's also just muddy the waters by implying that, since all languages have imperfections, talking about some other language's imperfections is just being pointlessly selective (even though one of the languages imperfections are much more horrible). It's just overall a non-argument. Maybe the point is to come off as being "above" the opposite camp (which are: mindless language bashers). But it's about as intellectually stimulating as mindless language bashing: it doesn't take much intellectual curiosity or rigour to bash a language, and neither does being perfectly neutral and non-offensive by throwing around platitudes like "no language is perfect", "choose the right tool for the job", etc.
- madisp 12y agoPointers.