By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

Need to return reference in this case?

P: n/a
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();

Jun 2 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
howa wrote:
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();

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 2 '07 #2

P: n/a
howa wrote:
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();
The second. "foo = new Foo" will create two copies of "Foo" in php4.

Any reason why you can't use php5?

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 2 '07 #3

P: n/a
On Jun 2, 7:51 pm, gosha bine <stereof...@gmail.comwrote:
The second. "foo = new Foo" will create two copies of "Foo" in php4.
what do you mean? you mean the second one is preferred?

why the first one will create two copies of "Foo" but not the first
one?

Any reason why you can't use php5?
php4 is stable, and its OOP function is enough (and simple) as far as
known.

Thanks.

Jun 2 '07 #4

P: n/a
At Sat, 02 Jun 2007 06:44:46 -0700, howa let h(is|er) monkeys type:
On Jun 2, 7:51 pm, gosha bine <stereof...@gmail.comwrote:
>The second. "foo = new Foo" will create two copies of "Foo" in php4.

what do you mean? you mean the second one is preferred?

why the first one will create two copies of "Foo" but not the first
one?

>Any reason why you can't use php5?

php4 is stable, and its OOP function is enough (and simple) as far as
known.

Thanks.
Stable, ok. OOP = enough, I beg to differ. OOP in PHP4 is poor, if not
cr@pola. PHP5 unstable? (which I think you imply by your answer to Gosha's
question) Why? How? When?

I get that typical "argument without foundation" feeling....

Sh.
--
Schraalhans Keukenmeester - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

Jun 2 '07 #5

P: n/a
..oO(howa)
>Any reason why you can't use php5?

php4 is stable
So is PHP 5.
>and its OOP function is enough (and simple) as far as
known.
OOP in PHP 4 is a joke, I wouldn't even call it OOP. An object in PHP 4
is just an ugly hack, an array with some attached functions. In PHP 5
you get an idea of what OOP really means and what you can do with it.

I don't see any reason to stay with PHP 4 anymore, it's outdated.

Micha
Jun 2 '07 #6

P: n/a
On 6 3 , 12 06 , Schraalhans Keukenmeester
<Schraalh...@the.spamtrapexample.nlwrote:
Stable, ok. OOP = enough, I beg to differ. OOP in PHP4 is poor, if not
cr@pola. PHP5 unstable? (which I think you imply by your answer to Gosha's
question) Why? How? When?

I get that typical "argument without foundation" feeling....
Hello,

this is a little out of topic...i don't want to start a war on PHP4 vs
PHP5 :)

anyway, my $0.02...

1. if it ain't broken, don't fix it. just like many large web sites
are still using PHP4, Apache 1.x, MySQL 4.x etc ...
2. personally, i think PHP5 OOP is not a well developed, maybe there
will be great changes in PHP6, even we can move to PHP6 directly
somedays...but now, PHP 4 works quite well.
Jun 2 '07 #7

P: n/a
On 6 3 , 12 52 , Michael Fesser <neti...@gmx.dewrote:
>
OOP in PHP 4 is a joke, I wouldn't even call it OOP. An object in PHP 4
is just an ugly hack, an array with some attached functions. In PHP 5
you get an idea of what OOP really means and what you can do with it.
i think the point is : there is always more than one way to do it

even you talk to wordpress developers, they even hesitate to use OOP.

array with some attached functions worked in our setup, that's nothing
wrong

:)

Jun 2 '07 #8

P: n/a
howa wrote:
On 6 3 , 12 06 , Schraalhans Keukenmeester
<Schraalh...@the.spamtrapexample.nlwrote:
>Stable, ok. OOP = enough, I beg to differ. OOP in PHP4 is poor, if not
cr@pola. PHP5 unstable? (which I think you imply by your answer to Gosha's
question) Why? How? When?

I get that typical "argument without foundation" feeling....

Hello,

this is a little out of topic...i don't want to start a war on PHP4 vs
PHP5 :)

anyway, my $0.02...

1. if it ain't broken, don't fix it. just like many large web sites
are still using PHP4, Apache 1.x, MySQL 4.x etc ...
2. personally, i think PHP5 OOP is not a well developed, maybe there
will be great changes in PHP6, even we can move to PHP6 directly
somedays...but now, PHP 4 works quite well.

If that were the case, we'd still be programming in assembler, cobol and
rpg.

I agree with the others. OOP support in PHP4 is terrible. That in
itself is enough reason to upgrade. And only laziness prevents someone
from upgrading on their own server.

Shared hosting is a little different. There are some differences which
can break PHP4 scripts, especially poorly written ones. And to force
your customers to rewrite their scripts is not necessarily good.

It has nothing to to with "good enough".

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 2 '07 #9

P: n/a
howa wrote:
On Jun 2, 7:51 pm, gosha bine <stereof...@gmail.comwrote:
>The second. "foo = new Foo" will create two copies of "Foo" in php4.

what do you mean? you mean the second one is preferred?

why the first one will create two copies of "Foo" but not the first
one?
$foo = new Foo

does the following in php4:

1) a chunk of memory is allocated and object's constructor is called to
initialize this memory
2) another chunk of memory is allocated and everything from the first
chunk is copied to it
3) variable 'foo' is being set to address of this second chunk
4) the first chunk is being disposed.

Apart from being ineffective, this technique will apparently fail if an
object saves reference to itself ($this) in constructor or when you need
exactly one instance.
>
>Any reason why you can't use php5?

php4 is stable, and its OOP function is enough (and simple) as far as
known.
I agree that OO "features" of php5 are mostly useless and php4 has
everything one needs to write OO programs. However, I still won't
recommend it, exactly for the reason described above.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 2 '07 #10

P: n/a
On 6 3 , 6 42 , gosha bine <stereof...@gmail.comwrote:
howa wrote:
$foo = new Foo

does the following in php4:

1) a chunk of memory is allocated and object's constructor is called to
initialize this memory
2) another chunk of memory is allocated and everything from the first
chunk is copied to it
3) variable 'foo' is being set to address of this second chunk
4) the first chunk is being disposed.

Apart from being ineffective, this technique will apparently fail if an
object saves reference to itself ($this) in constructor or when you need
exactly one instance.

Hello,

Yes, but refer to my original question, both (1) & (2) have this
problem of inefficieny.

So you said (2) is perfferred?

Why? Since PHP said return by reference is mostly useless.
I agree that OO "features" of php5 are mostly useless and php4 has
everything one needs to write OO programs. However, I still won't
recommend it, exactly for the reason described above.
This is not a big problem in fact, people can upgrade to PHP5, but
still using PHP4 syntax. We don't like PHP5 since it is not well
planned, and subject to change.

Jun 3 '07 #11

P: n/a
On Jun 2, 11:14 pm, howa <howac...@gmail.comwrote:
On 6 3 , 6 42 , gosha bine <stereof...@gmail.comwrote:
howa wrote:
$foo = new Foo
does the following in php4:
1) a chunk of memory is allocated and object's constructor is called to
initialize this memory
2) another chunk of memory is allocated and everything from the first
chunk is copied to it
3) variable 'foo' is being set to address of this second chunk
4) the first chunk is being disposed.
Apart from being ineffective, this technique will apparently fail if an
object saves reference to itself ($this) in constructor or when you need
exactly one instance.

Hello,

Yes, but refer to my original question, both (1) & (2) have this
problem of inefficieny.

So you said (2) is perfferred?

Why? Since PHP said return by reference is mostly useless.
I agree that OO "features" of php5 are mostly useless and php4 has
everything one needs to write OO programs. However, I still won't
recommend it, exactly for the reason described above.

This is not a big problem in fact, people can upgrade to PHP5, but
still using PHP4 syntax. We don't like PHP5 since it is not well
planned, and subject to change.
That sounds wrong to me. PHP5's OOP is useless only if you don't have
any use for OOP in the first place. You want a struct, use PHP4 OOP,
fine. But if you want actual OOP, use PHP5. I know I couldn't use PHP4
for the database framework I wrote in 5 based on an abstract
superclass.

~A!

Jun 3 '07 #12

P: n/a
howa wrote:
On 6 3 , 6 42 , gosha bine <stereof...@gmail.comwrote:
>howa wrote:
$foo = new Foo

does the following in php4:

1) a chunk of memory is allocated and object's constructor is called to
initialize this memory
2) another chunk of memory is allocated and everything from the first
chunk is copied to it
3) variable 'foo' is being set to address of this second chunk
4) the first chunk is being disposed.

Apart from being ineffective, this technique will apparently fail if an
object saves reference to itself ($this) in constructor or when you need
exactly one instance.


Hello,

Yes, but refer to my original question, both (1) & (2) have this
problem of inefficieny.

So you said (2) is perfferred?

Why? Since PHP said return by reference is mostly useless.
function getFoo()...
$foo = Foo::getFoo();

involves copying as I said above

function &getFoo()..
$foo =& Foo::getFoo();

does not involve copying, only one memory block is created


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 3 '07 #13

P: n/a
At Sat, 02 Jun 2007 10:28:30 -0700, howa let h(is|er) monkeys type:

>
i think the point is : there is always more than one way to do it

even you talk to wordpress developers, they even hesitate to use OOP.
With al due respect to them (WP developers), have you ever studied the
Wordpress core code? Their hesitation to go OOP seems to be
self-explanatory glancing over their stuff. I had visions of the MIR
space-station innards. The damn thing worked, but oh boy, you don't want
to see inside how it's achieved.

But I don't want to start a flamewar here either. I'll agree to disagree.
Rgds

--
Schraalhans Keukenmeester - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

Jun 3 '07 #14

P: n/a
On Jun 3, 6:42 pm, gosha bine <stereof...@gmail.comwrote:
howa wrote:
On 6 3 , 6 42 , gosha bine <stereof...@gmail.comwrote:
howa wrote:
$foo = new Foo
does the following in php4:
1) a chunk of memory is allocated and object's constructor is called to
initialize this memory
2) another chunk of memory is allocated and everything from the first
chunk is copied to it
3) variable 'foo' is being set to address of this second chunk
4) the first chunk is being disposed.
Apart from being ineffective, this technique will apparently fail if an
object saves reference to itself ($this) in constructor or when you need
exactly one instance.
Hello,
Yes, but refer to my original question, both (1) & (2) have this
problem of inefficieny.
So you said (2) is perfferred?
Why? Since PHP said return by reference is mostly useless.

function getFoo()...
$foo = Foo::getFoo();

involves copying as I said above

function &getFoo()..
$foo =& Foo::getFoo();

does not involve copying, only one memory block is created
Good, back to the real problem!

:)

but according to PHP, the opimizer is smart to handle this?
>>Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own
http://hk.php.net/manual/en/language...ces.return.php

any comments?

thanks.
Jun 4 '07 #15

P: n/a
On Jun 2, 11:23 am, howa <howac...@gmail.comwrote:
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();
I didn't really read the follow-ups, but you should take care of the
typos first. This ain't no singleton. $instace != $instance

Jun 4 '07 #16

P: n/a
On 04.06.2007 15:31 howa wrote:
On Jun 3, 6:42 pm, gosha bine <stereof...@gmail.comwrote:
>howa wrote:
>>On 6 3 , 6 42 , gosha bine <stereof...@gmail.comwrote:
howa wrote:
$foo = new Foo
does the following in php4:
1) a chunk of memory is allocated and object's constructor is called to
initialize this memory
2) another chunk of memory is allocated and everything from the first
chunk is copied to it
3) variable 'foo' is being set to address of this second chunk
4) the first chunk is being disposed.
Apart from being ineffective, this technique will apparently fail if an
object saves reference to itself ($this) in constructor or when you need
exactly one instance.
Hello,
Yes, but refer to my original question, both (1) & (2) have this
problem of inefficieny.
So you said (2) is perfferred?
Why? Since PHP said return by reference is mostly useless.
function getFoo()...
$foo = Foo::getFoo();

involves copying as I said above

function &getFoo()..
$foo =& Foo::getFoo();

does not involve copying, only one memory block is created

Good, back to the real problem!
And the real problem is.......?
but according to PHP, the opimizer is smart to handle this?
to handle what?
>
>>Do not use return-by-reference to increase performance, the engine is smart enough to optimize this on its own
please also read the NEXT phrase ;)
>
http://hk.php.net/manual/en/language...ces.return.php

any comments?
Yet again, "$a = new Foo" actually creates TWO objects in php4. The
first one is an anonymous object created by "new" and the second one is
the object the variable "$a" points to. This behavior is ineffective AND
it doesn't work in certain cases, e.g singletons.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 4 '07 #17

P: n/a
On Jun 4, 10:34 pm, gosha bine <stereof...@gmail.comwrote:
Yet again, "$a = new Foo" actually creates TWO objects in php4. The
first one is an anonymous object created by "new" and the second one is
the object the variable "$a" points to. This behavior is ineffective AND
it doesn't work in certain cases, e.g singletons.
but using a static function variable made singleton possible (in
PHP4), as my example above, both ways are working...

Jun 4 '07 #18

P: n/a
On 04.06.2007 17:58 howa wrote:
On Jun 4, 10:34 pm, gosha bine <stereof...@gmail.comwrote:
>Yet again, "$a = new Foo" actually creates TWO objects in php4. The
first one is an anonymous object created by "new" and the second one is
the object the variable "$a" points to. This behavior is ineffective AND
it doesn't work in certain cases, e.g singletons.

but using a static function variable made singleton possible (in
PHP4), as my example above, both ways are working...
Just test it

- - - - - - - -

class Foo {
var $x;
function /*&*/getFoo() {
static $instance;
if (!isset($instance))
$instance = new Foo();
return $instance;
}
}

$a = /*&*/Foo::getFoo();
$b = /*&*/Foo::getFoo();

$a->x = 123;

# $b->x should be 123 too
# because it's a SINGLETON

assert($b->x == 123); // FAILURE

- - - - - - - -

Run it. Did it fail? Do you understand why?

Now, uncomment all references and try again.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 4 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.