- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Base->self($Frist)->inline(my_custom_print($Frist))
Admin
I don't think this actually works, at least not with statements like
echo
. PHP functions are allowed to be called with more arguments than they are defined with, so you can safely put expressions in the parentheses and they will be evaluated. An assignment is an expression, whereas anecho
isn't.Admin
Invoke not the gods of programming. For they are terrible and nameless. And if you gaze for long into the code, the code gazes also into you. And you would do well not to attract its ire.
Admin
The best idea I can come up with, the expression in the call to
inline
gets evaluated, but the result is not actually passed intoinline
method and gets discarded. However, that expression has a side effect - it sets a field (or whatever that field setting translates to with PHP magic), soinline
is a clever way to write a variable assignment as a chained call. Why would you do that? No idea.Like a few other times on this website, I get the impression that the author was under the influence of some kind of narcotics.
Addendum 2024-09-16 07:39: Looks like Remy reached the same conclusion.
Admin
<quote>now please don't look at my C++ template code that implements compile time loops to automagically generate chains of function calls to traverse typelists and thus allow declarative bindings between message types and functions which receive those message types.</quote>
Well this would definitely qualify as 'Clever' code in my book...
Admin
Just because you can do this sort of thing doesn't mean that you should.
Admin
Sounds like someone had used a better language like Ruby or Python and wanted to add that "magic" to PHP.
Admin
I would kick a developer off my team for saying that seriously. No, you fscking cannot let that slide.
Admin
PHP does at least have a defined behaviour for falling off the end of a function without an explicit return - it returns
null
. (That is, the closing brace of a function contains an implicitreturn;
orreturn null;
. Apparently, that's the case even if there's an explicitreturn;
.)So yes, it's sloppy, but it has defined behaviour.
Addendum 2024-09-16 09:13: And I'm not a fan of developers being sloppy either, but this is PHP...
Admin
In this case, I mean it more in the sense of "there are worse things to discuss". Yes, it's bad, but it's hardly a WTF.
Admin
The quality of that codebase could be improved by equipping the author of that code with a straitjacket, so he cannot type on a keyboard.
Admin
The
inline()
method exists so you can perform arbitrary expression evaluation in a one-liner using the fluent interface.It works because argument expressions are always evaluated.
Re @no's comment about
echo
being a statement rather than expression, you can useprint()
instead. You can' useif
statements, but you can use a ternary. PHP 8.0 has also addedmatch
, which is toswitch
as ternary is toif
. So the only kind of statement you can't execute this way is a loop.Admin
/* You are not expected to understand this. */
Admin
Good programming magic is something a beginner wouldn't have realized could be written, but can easily use once it exists. Bad programming magic makes it impossible for a beginner to understand code that uses it.
Q3 Fast InvSqrt is good magic. Even the most basic programmer can see "this is a function, you give it a float x and it returns a float that is 1/sqrt(x)", and use it productively. But understanding how it works requires a lot of arcane knowledge of IEEE754 and logarithms and honestly, I'm still not 100% sure how it works.
This? This is bad magic, because instead of hiding the tricky parts, it's made them necessary to understand. Instead of providing a tool for less PHP-savvy coders to use, it's made a codebase that less PHP-savvy coders cannot work in. Even as a pro PHP dev, I'm confused what it's even trying to do.
Admin
What this is is lazy instantiation of the widgets. I will not address the inline bit as I don't speak PHP.
Admin
The base class methods are very clever - and rather evil. To break it down:
self
could also be calledassignTo
- it takes anout
variable, so the call->self($w)
is actually declaring the variable$w
(which we will then assign to in the body ofself
.self
has returned we now have a newly declared variable$w
available in the enclosing scope that is a reference to thethis
ofself
's scope (yayout
parameters ...</un-enthused>
).->inline()
, but$w
is available for the rest of the enclosing function so we don't need to limit ourselves in any way.It's kind of like Groovy and Kotlin's implicit
it
variable for closures, but using the enclosing scope rather than a new language feature. For what it's worth this abomination is not limited to PHP; in recent versions of C# you could also do this: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters#:~:text=You%20can%20also%20declare%20the%20out%20variable%20in%20the%20argument%20listAdmin
Perl's default return is to return the last expression evaluated. This was convenient, but confusing to many people. And also a bit weird... there is an explicit return, but it basically has the expense of a non-local exit (think "handling an exception"), even when it was the last expression in the subroutine. So, adding explicit returns at the end of your functions actually slowed it down slightly.
Admin
In C#, there're both the out keyword and the type. Comparing it to PHP is invalid
Admin
The indentation of the first code snippet is... surprising. The "non-reading eye" assumes that the function getStatus() is somehow inside the function getOtherWidgets(), and the if(isset(.. thing is on the same leven as getStatus(), not inside it, and also part of getOtherWidgets(), which it isn't. The comments further down are also quite interesting: The two slashes and the left stroke of the "A" create a nice slope, at least in the "Attach variable name" line; in the next one, the "Allows you to preform" line, it's one off. How typical...
Admin
Cricky.... Haven't seen black-magic like this since the time I tried collaborating on some JS code a 'clever' programmer was working on. In my time doing PHP (PHP4 / 5 days) I developed some interesting code, but this goes even next level.
Something tells me that the person that wrote this has (at least) a JS background.
Admin
Some people have mentioned it in passing, but let me just focus on this for a moment. This is complete legit:
function x() { } x( $a=42 ); echo $x; // Outputs 42
The reason this works is because in The Olden Days™ this was how you did variadic functions in PHP - much like the "arguments" object in Javascript. Today there's a better syntax for it, but this still works and isn't even deprecated. The expression is evaluated and is passed into the function - where you can access it via a call to func_get_args().
Of course what the given code does is just abuse of the language.