2016-09-17 20:11
浏览 58


Need some help understanding what's going on in my PHP code.


$cache = new Cache();


class Cache {
    private static $URL_CACHE;

    public function cache($url, $entityId) {
        echo '<br>caching '.$url.' as '.$entityId;

When I request index.php, I get 'caching as' displayed, which is a surprise. I never called $cache->cache('', '');

What's calling the method?

As per Blake's answer, since the method name matches (somewhat) the class name, it's called on instantiation. For Java developers this will certainly be a surprise.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duancong7358 2016-09-17 20:15

    This is using deprecated PHP functionality to act as a __contruct() method. In older versions of PHP (Removed in 7) If you had a class named Foo and a function named foo() it was how you would call it as a constructor.

    In short, this is being called by you instantiating the class. If you change your cache() method to makeCache() I bet it will go away.

    Another solution is to have an empty constructor as well, thanks JimL.

    public function __construct() {
    打赏 评论

相关推荐 更多相似问题