返回值的类型是对象的时候,是否需要使用引用返回?

今天在看框架,看到入口bootstrap文件.这个是一个工厂类模式,通过option来创建对应的object.
负责实例具体对象的那个工厂方法大致是这样的:

class bootstrap {
...
public static function &factory($options = array()) {

        if (!(self::$_instance instanceof self)) {

            if ($options['interface'] == 'web') {
                $interface = 'Web';
            } else if ($options['interface'] == 'cli') {
                $interface = 'Cli';
            } 
        }
        self::$_instance = new $interface();

        return self::$_instance;
    }
...
}


看到这个factory方法是使用的引用返回.但有的地方在调用的时候是这样的 &bootstrap::factory(); 有的是这样的 bootstrap::factory(); ,运行起来也没有什么不对的.
于是我就想一探究竟.
于是有下面的一堆代码:

';
        return self::$int;
    }
    
    private function __construct(){
        echo 'bar __construct function 
'; } } //$a = new bar(); //错误示范 //不使用&进行引用返回的调用 $b = bar::int(); var_dump($b); echo "
"; if( $b instanceof bar ){ echo '$b instanceof bar
'; } else{ echo 'no bar
'; } echo "------------------------------
"; //使用引用返回 $c = &bar::int(); var_dump($c); echo "
"; if( $c instanceof bar ){ echo '$c instanceof bar
'; } else{ echo 'no bar
'; } echo "
"; class foo { private static $int = null; //普通定义 public static function int(){ if(!self::$int){ self::$int = new self(); } echo 'foo int function
'; return self::$int; } private function __construct(){ echo 'foo __construct function
'; } } $j = foo::int(); var_dump($j); echo "
"; if( $j instanceof foo ){ echo '$j instanceof foo
'; } else{ echo 'no foo
'; } echo "------------------------------
"; $k = foo::int(); var_dump($k); echo "
"; if( $k instanceof foo ){ echo '$k instanceof foo
'; } else{ echo 'no foo
'; } echo "
"; class test { public function __construct(){ echo "test construct
"; } } $z = new test(); var_dump($z); if($z instanceof test){ echo '$z instanceof test
'; } echo "------------------------------
"; $y = new test(); var_dump($y); if($y instanceof test){ echo '$y instanceof test
'; }

结果:
bar __construct function
bar int function
object(bar)[1]
$b instanceof bar
——————————
bar int function
object(bar)[1]
$c instanceof bar


foo __construct function
foo int function
object(foo)[2]
$j instanceof foo
——————————
foo int function
object(foo)[2]
$k instanceof foo


test construct
object(test)[3]
$z instanceof test
——————————
test construct
object(test)[4]
$y instanceof test

为了严谨些,我有做了下面的测试:

';
        
        if(!(self::$_instance instanceof self)){
            self::$_instance = new $type();
        }
        return self::$_instance;
    }
    
    protected function __construct(){
        echo 'class a construct
'; } } class b extends a { public function __construct(){ parent::__construct(); echo 'class b construct
'; } } class c extends a { public function __construct(){ parent::__construct(); echo 'class c construct
'; } } $bObject = a::factory('b'); var_dump($bObject); $b2Object = &a::factory('b'); var_dump($b2Object); echo '
'; class aa { protected static $_instance; public static function factory($type){ echo 'class aa function factory
'; if(!(self::$_instance instanceof self)){ self::$_instance = new $type(); } return self::$_instance; } protected function __construct(){ echo 'class aa construct
'; } } class bb extends aa { public function __construct(){ parent::__construct(); echo 'class bb construct
'; } } class cc extends aa { public function __construct(){ parent::__construct(); echo 'class cc construct
'; } } $bbObject = aa::factory('bb'); if( $bbObject instanceof aa ){ echo '$bbObject instanceof aa
'; } else{ echo 'NO $bbObject instanceof aa
'; } if( $bbObject instanceof bb ){ echo '$bbObject instanceof bb
'; } else{ echo 'NO $bbObject instanceof bb
'; } var_dump($bbObject); $bb2Object = aa::factory('bb'); var_dump($bb2Object);

output:
class a function factory
class a construct
class b construct
object(b)[1]
class a function factory
object(b)[1]


class aa function factory
class aa construct
class bb construct
$bbObject instanceof aa
$bbObject instanceof bb
object(bb)[2]
class aa function factory
object(bb)[2]

相关文章

发表评论

返回顶部