PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 7, 2022

[FIXED] Return conditional on method using array and/or object

 January 07, 2022     php, wordpress     No comments   

Issue

So I have a single method that generated a cache key and also applies a transient automatically.

Here is the method:

private function get_cache($id, $count)
{
    $cache_key = $this->generate_cache_key($id, $count);

    return get_transient($cache_key);
}

How could I make that method return both the $cache_key but also get_transient?

Here is what I'm trying to achieve:

  • Access the $cache_key inside another method.
  • Also execute the get_transient when calling the method.

I have a method and this is what I'm aiming to achieve:

public function delete_cache($count = 4)
    {
        $cache_key = $this->get_cache['cache_key'];
        var_dump($cache_key);
    }

So I was thinking something like $instagram->get_cache['cache_key'] but also keep the original functionality for:

if ($cached = $instagram->get_cache($instagram->user_id, $count)) {
    return $cached;
}

Does anyone know how I can get the cache_key for another method, but still keep the get_transient return?


Solution

The concept of returning multiple values from a function is called a "tuple". Almost every language implements this to some degree, sometimes as a "record", sometimes as a "database row", or maybe as a struct. For PHP, you are pretty much limited to either an object with fields, or an array, with the latter being the most common. Your get_cache function could be reworked as:

    private function get_cache($id, $count)
    {
        $cache_key = $this->generate_cache_key($id, $count);

        return [$cache_key, get_transient($cache_key)];
    }

And to invoke it you'd do:

[$cache_key, $value] = $this->get_cache('a', 4);

Or, if using an older version of PHP (or you just don't like the look of that):

list($cache_key, $value) = $this->get_cache('a', 4);

The downside of this is that all callers have to be changed to support this, which may or may not be a problem. An alternative is to add an optional callback to the function that performs more work:

    private function get_cache($id, $count, callable $func = null)
    {
        $cache_key = $this->generate_cache_key($id, $count);

        $value = get_transient($cache_key);

        if(is_callable($func)){
            $func($cache_key, $value);
        }

        return $value;
    }

And call it like:

$value = $this->get_cache(
        'a',
        4,
        static function($cache_key, $value) {
            var_dump($cache_key);
        }
    );

Although you are using WordPress, I think it is helpful to see what other frameworks do, too. PSR-6 defines something called CacheItemInterface which is the object-form of the return, and Symfony's cache (which you can actually use in WordPress, I do sometimes on large projects) uses the get-with-callback syntax.



Answered By - Chris Haas
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing