在这种情况下需要返回参考? [英] Need to return reference in this case?

查看:87
本文介绍了在这种情况下需要返回参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的单例类(PHP4)


哪种方式优先?


// 1.

class Foo {


函数getFoo(){

static $ instace;

if(!isset($ instace)) {

$ instance = new Foo();

// ...

}

返回$ instance ;

}

}


$ foo = Foo :: getFoo();


// 2.


class Foo {


function& getFoo(){

static $ instace;

if(!isset($ instace)){

$ instance = new Foo();

// ...

}

返回$ instance;

}

}


$ foo =& Foo :: getFoo();

解决方案

instace;

if(!isset(

instace)){


instance = new Foo();

// ... ...

}

返回


a simple singleton class (PHP4)

which way is preffered?

// 1.
class Foo {

function getFoo() {
static $instace;
if (!isset($instace) ) {
$instance = new Foo();
// ...
}
return $instance;
}
}

$foo = Foo::getFoo();

// 2.

class Foo {

function &getFoo() {
static $instace;
if (!isset($instace) ) {
$instance = new Foo();
// ...
}
return $instance;
}
}

$foo =& Foo::getFoo();

解决方案

instace;
if (!isset(


instace) ) {


instance = new Foo();
// ...
}
return


这篇关于在这种情况下需要返回参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆