PHP Getting a reference through a constructor?

前端 未结 2 693
遥遥无期
遥遥无期 2021-01-23 10:51

I am wondering if it is possible while using the keyword \'new\' to return a reference to an existing object instead of a new object. And if not, what would be the best way to a

2条回答
  •  终归单人心
    2021-01-23 11:30

    Wouldn't it just be easier to directly store the object references in your instances var?

    if (!isset(self::$instances[$myparam])) {
        self::$instances[$myParam] = ... new object here ...;
    } else
        return &self::$instances[$myParam];
    }
    

    Just guessing, but it seems that storing your simple integer parameter as an array key to the instances cache would be "cheaper" than storing an sha1 signature of a serialized structure that could potentially NOT be the same even if the object has the same initial parameter.

提交回复
热议问题