Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Basic, non Vanilla related OOP question

R_JR_J Cheerleader & TroubleshooterMunich Moderator

The first real code I wrote has been in VBS for Excel - yeah, you laugh... But there I have learned that it is nearly always better to create a variable than using a long "path" like that: set sheet = Application.CurrentWorkbook.ActiveSheet and from then on work only with "sheet".

I've tried to google for that, but I do not know the right words to look for. What would be better:

$value = Gdn::cache()->get($key);
...
    Gdn::cache()->store($key, $value);

or

$cache = Gdn::cache();
$value = $cache->get($key);
...
    $cache->store($key, $value);

or even

$cache = new Gdn_Cache();
$value = $cache->get($key);
...
    $cache->store($key, $value);

Or isn't there any difference at all?

Comments

  • peregrineperegrine MVP
    edited August 2015

    I like variables since it make it easier to read, correct and change.
    But I suspect it is femtoseconds faster if there is no need for the intermediate step and storing the value.

    The same reason I hate ternarys - harder to modify and makes things cute and obfuscated.

    maybe when there were stroage problems, memory restrictions, etc. it was great to use reduction. But these days, a bit of clarity might be the overriding factor.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • LincLinc Vanilla's Bard (and Director of Development) Detroit Vanilla Staff

    In Vanilla, always use the static reference instead of assigning it to a variable. It's just memory overhead and, in our opinion, adds no clarity.

    R_J
  • mtschirsmtschirs ✭✭✭
    edited August 2015

    Long delegation chain or scope chain optimization would be the name of the phenomenon you describe (at least that is what the C++ / JS guys call it).

    Even in C++, a chain such as objectA->objectB->value would require dereferencing two pointers at runtime, so if you need to access value very often, better store it in a temporary variable or create a temporary reference.
    In PHP, due to the dynamic nature of the language that allows you to add and remove fields and methods to objects at runtime, the lookup performed each time you use the "->" operator is surely much more costly.

    Accessing a field via the "::" scope resolution operator however is cheaper, since (as far as I know) the runtime lookups involved are simpler. That is because "::" only allows you to access static fields or static methods declared at compile time. However, accessing a singleton via $value = Gdn::cache(); involves a method call - and method calls are slow in PHP.

    Still, in the big picture, these performance gains might be neglectible. But not always, see https://blog.engineyard.com/2014/profiling-with-xhprof-xhgui-part-3

    -

    R_Jperegrine
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    That's very insightful! Many, many thanks!

Sign In or Register to comment.