U
U
unfapable2016-03-31 06:07:50
PHP
unfapable, 2016-03-31 06:07:50

How should the state of an object be stored?

Probably, the title of the question is difficult to understand something, I will try to explain so that it is clear.
The task was to check for the entry of the current IP in the list of exceptions. This is done by simply calling the static method (IpAddressExclusion::isExcluded), which iterates over the IP array property (it is hard-coded in the class, bad, but that's not the point now), and sees if the user's current IP is in this array. But the problem is that this method call is called several times on the page, and now it turns out that the same check is done every time, even if it has already been carried out, but isn't that bad?
For example, there is a call to this method in the site header, then in the sidebar, and then in the footer - accordingly, if the check was performed in the header, then there is no point in doing it in the sidebar and footer, but you just need to return the saved state (that is, a log value, which was saved in the property during the first check in the header)? If so, how to do it correctly? Do I understand correctly that a singleton is needed here? Those. in the isExcluded method, you need to save the state of the check and then check if this property is not null, then return it? But then something like this construction will turn out: IpAddressExclusion::getInstance()->isExcluded()? This is normal, it's embarrassing that the getInstance method turns out to be static, although it shouldn't, right?
I hope it’s clear what I want, I’m still a beginner, but I really want to do it in a normal way, so I painted everything like this :/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2016-03-31
@unfapable

getInstance is an instance of an object (it is also needed), and it just needs to be static:

$a = MyClass::getInstance();
$b = MyClass::getInstance();

'a' and 'b' will have the same object.
---
To save the property, you need something like this code:
class C
{
  private $p;

  public function getProperty() {
    if (is_null($this->p)) {
      $this->p = ...
    }
    return $this->p;
  }
}

---
Singleton implementation in a simple form:
class C
{
  private static $instance;

  private function __constructor() {
    // init
  }

  public static function getInstace() {
    if (is_null($this->instance)) {
      $this->instance = new self();
    }
    return $this->instance;
  }
}

D
DieZz, 2016-03-31
@DieZz

You are calling a static method on a class, so a singleton is not appropriate here, since a singleton returns an object and a static method points to a class . Yes, and run through the array and find out if there is an IP is not a costly operation. PHP works quite fast with arrays. To me, this is optimization for the sake of optimization.
upd :

class IpAddressExclusion
{
    static private $instance;
    private $cache;
    private $excluded = [
        '192.168.1.1',
        '192.168.1.2',
        '192.168.1.3',
    ];

    private function __construct()
    {

    }

    private function setCache($key, $value)
    {
        if (!isset($this->cache[$key])) {
            $this->cache[$key] = $value;
        }
    }

    private function getCache($key)
    {
        if ($this->hasCache($key)) {
            return $this->cache[$key];
        }

        return null;
    }

    private function hasCache($key)
    {
        return isset($this->cache[$key]);
    }

    public function isExcluded($ip)
    {
        $search = array_search($ip, $this->excluded);
        if ($search && $search !== 0) {
            if ($this->hasCache($ip)) {
                return $this->getCache($ip);
            } else {
                $this->setCache($ip, true);
            }

            return true;
        }

        return false;
    }

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new IpAddressExclusion();
        }

        return self::$instance;
    }
}

Then use like this:
if (IpAddressExclusion::getInstance()->isExcluded('192.168.1.1')) {
    //Делам что то
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question