PHP通过构造函数获取引用? [英] PHP Getting a reference through a constructor?
问题描述
我想知道在使用关键字"new"返回对现有对象而不是新对象的引用时是否可能.如果不是,那么避免具有相同参数的对象共存的最佳方法是什么?
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 avoid co-existence of objects with the same parameters?
我在构造函数上有一个带有参数的类.如果我使用的参数类似于现有实例的参数,那么我希望新对象是对现有实例的引用.是否有可能?我知道它不同于单例,因为在这种情况下,我们可以有多个相同类的实例,但不能有多个使用相同参数的实例.
I have a class with arguments on the constructor. If I am using arguments similar to the ones of an existing instance, I'd like my new object to be a reference to the existing one. Is it possible? I understand it is different than a singleton as in this case we can have several instances of the same class, but not several instances using the same parameters.
class myClass{
public $signature; /* Unique signature for the connection */
public static $instances = array(); /* Array of references to connection */
private static function singleton($cfg,$inst){
$signature = sha1(serialize($cfg));
foreach ( self::$instances as $obj )
{
if ( $obj->signature == $signature )
return $obj;
}
array_push(self::$instances,$inst);
return true;
}
function __construct($myParam){
if ( is_object(self::singleton($myParam,$this) )
// Here I'd like $this to become the ref of my existing object
else
{
$this->signature = $myParam;
// I construct my new object
}
}
}
$e1 = new myClass(1);
$e2 = new myClass(2);
$e3 = new myClass(1); // $e3 should be a reference to $e1
推荐答案
首先的几个事实:
- 构造函数永远不能返回值.
- $ this不能重新分配(它可以在PHP 4中使用,但不是故意的,并且没有记录)
结论:
- 使用工厂方法或委派.*
*两者之间有细微差别,即使用委托$ a!= $ b,但使用工厂方法$ a === $ b(见下文)
* There is a slight difference between the two, and that is that using delegation $a!=$b, but using factory methods $a === $b (see below)
委派示例:
// Declare the delegate that will be wrapped by DelegationClass
// * Only public methods/properties will be accessible
class MyClass {
public $a;
public function a($a) {
echo "This is a(\$a=$a)\n";
}
}
class MyDelegationClass {
static protected $_delegation = 'MyClass'; // define the delegate
static protected $_i = array(); // instances
protected $_l = null; // delegation link
public function __construct($n) {
if (!array_key_exists($n,self::$_i)) {
// ensures that the instance is always created
self::$_i[$n] = new self::$_delegation;
}
$this->_l = self::$_i[$n]; // link to delegate
}
public function __get($v) {
return $this->_l->$v; // get property from delegate link
}
public function __set($k,$v) {
return $this->_l->$k = $v; // set property on delegate link
}
public function __call($f,$p) {
// call method on delegate link
return call_user_func_array(array($this->_l,$f),$p);
}
}
// $a != $b, but $a->a === $b->a
$a = new MyDelegationClass(1);
$b = new MyDelegationClass(1);
工厂类示例:
class MyFactoryClass {
static protected $_i = array(); // instances
public static function newMyClass($n) {
if (!array_key_exists($n,self::$_i)) {
// ensures that the instance is always created
self::$_i[$n] = new MyClass;
}
return self::$_i[$n]; // return instance
}
}
// $a === $b
$a = MyFactoryClass::newMyClass(1);
$b = MyFactoryClass::newMyClass(1);
这篇关于PHP通过构造函数获取引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!