0

I have these two classes:

class Service {
    public static function __callStatic($name, $arguments)
    {
        // ... opt-out code
        $result = call_user_func_array([CacheService::class, $name], $arguments);
        // ... opt-out code
    }
}

And this

class CacheService
{
    public static function __callStatic($name, $arguments)
    {
        // ... opt-out code
        if (self::getCacheInstance()->has('some_cache_key')) {
            return call_user_func_array(['self', $name], $arguments);
        }
        // ... opt-out code
    }

    public static function getItems()
    {
        //... do operations
    }
}

When I call Service::getItems(); from the controller, it executes __callStatic in Service class, but when Service class attempts to call getItems() from CacheService, it does not execute __callStatic in CacheService class. What is the problem exactly ?

1 Answer 1

2

__callStatic is only executed when there is no static method with the calling method name

Your Service class does not contain a getItems() method so __callStatic gets executed.

Your CacheService does contain it so getItems gets executed instead

http://php.net/manual/en/language.oop5.overloading.php#object.callstatic

Example:

<?php

class A {
    public static function __callStatic() {
        echo "A::__callStatic";
    }
}

class B {
    public static function __callStatic() {
        echo "B::__callStatic";
    }

    public static function getItems() {
        echo "B::getItems";
    }
}

A::getItems(); // A::__callStatic
B::getItems(); // B::getItems()
B::anotherFunction(); // B::__callStatic
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.