Share variables between functions in PHP without using globals

后端 未结 4 1378
余生分开走
余生分开走 2021-01-03 05:49

I have a class for interacting with a memcache server. I have different functions for inserting, deleting and retrieving data. Originally each function made a call to

4条回答
  •  萌比男神i
    2021-01-03 06:05

    Pass in the MC instance:

    class abc
    {
        function __construct($mc) {
            $this->mc = $mc;
        }
    }    
    class def
    {
        function __construct($mc) {
            $this->mc = $mc;
        }
    }
    
    $mc = new cache_class;
    $abc = new abc($mc);
    

    etc.

提交回复
热议问题