在这种情况下需要返回参考? [英] Need to return reference in this case?
问题描述
一个简单的单例类(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屋!