• Frist (unregistered)

    Base->self($Frist)->inline(my_custom_print($Frist))

  • no (unregistered)

    Somehow, inline doesn't take parameters, but a statement in the parentheses gets evaluated. I can't accurately explain how this works.

    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 an echo isn't.

  • LZ79LRU (unregistered)

    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.

  • (nodebb)

    The best idea I can come up with, the expression in the call to inline gets evaluated, but the result is not actually passed into inline 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), so inline 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.

  • Yazeran (unregistered)

    <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...

  • (nodebb)

    Just because you can do this sort of thing doesn't mean that you should.

  • (nodebb)

    Sounds like someone had used a better language like Ruby or Python and wanted to add that "magic" to PHP.

  • (nodebb)

    So, getStatus doesn't always return a value. [...] we can let that slide

    I would kick a developer off my team for saying that seriously. No, you fscking cannot let that slide.

  • (nodebb) in reply to dpm

    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 implicit return; or return null;. Apparently, that's the case even if there's an explicit return;.)

    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...

  • (author) in reply to dpm

    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.

  • Sauron (unregistered)

    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.

  • (nodebb)

    The inline() method exists so you can perform arbitrary expression evaluation in a one-liner using the fluent interface.

    $instance->inline($x = 1)->inline->inline(call_some_func());
    

    It works because argument expressions are always evaluated.

    Re @no's comment about echo being a statement rather than expression, you can use print() instead. You can' use if statements, but you can use a ternary. PHP 8.0 has also added match, which is to switch as ternary is to if. So the only kind of statement you can't execute this way is a loop.

  • BeeKay (unregistered)

    /* You are not expected to understand this. */

  • gman003 (unregistered)

    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.

  • Loren Pechtel (unregistered)

    What this is is lazy instantiation of the widgets. I will not address the inline bit as I don't speak PHP.

  • NotYourLocalPhpWizard (unregistered)

    The base class methods are very clever - and rather evil. To break it down:

    1. self could also be called assignTo - it takes an out variable, so the call ->self($w) is actually declaring the variable $w (which we will then assign to in the body of self.
    2. We're relying on the side effect of setting that variable completing before the next element in the chain is invoked. So after self has returned we now have a newly declared variable $w available in the enclosing scope that is a reference to the this of self's scope (yay out parameters ...</un-enthused>).
    3. We can now use that newly declared variable in subsequent expressions (including ->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%20list

  • Randal L. Schwartz (github) in reply to Steve_The_Cynic

    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 implicit return; or return null;. Apparently, that's the case even if there's an explicit return;.)

    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.

  • (nodebb) in reply to Randal L. Schwartz

    In C#, there're both the out keyword and the type. Comparing it to PHP is invalid

  • Officer Johnny Holzkopf (unregistered)

    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...

  • Daemon (unregistered)

    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.

  • Vilx- (unregistered)

    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.

Leave a comment on “A Clever Base”

Log In or post as a guest

Replying to comment #:

« Return to Article