473,788 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to return reference in this case?

a simple singleton class (PHP4)

which way is preffered?

// 1.
class Foo {

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

$foo = Foo::getFoo();

// 2.

class Foo {

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

$foo =& Foo::getFoo();

Jun 2 '07 #1
18 1666
howa wrote:
a simple singleton class (PHP4)

which way is preffered?

// 1.
class Foo {

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

$foo = Foo::getFoo();

// 2.

class Foo {

function &getFoo() {
static $instace;
if (!isset($instac e) ) {
$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
howa wrote:
a simple singleton class (PHP4)

which way is preffered?

// 1.
class Foo {

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

$foo = Foo::getFoo();

// 2.

class Foo {

function &getFoo() {
static $instace;
if (!isset($instac e) ) {
$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
On Jun 2, 7:51 pm, gosha bine <stereof...@gma il.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
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...@gma il.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 .Spamtrapexampl e.nl
[Remove the lowercase part of Spamtrap to send me a message]

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

Jun 2 '07 #5
..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
On 6 3 , 12 06 , Schraalhans Keukenmeester
<Schraalh...@th e.spamtrapexamp le.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
On 6 3 , 12 52 , Michael Fesser <neti...@gmx.de wrote:
>
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
howa wrote:
On 6 3 , 12 06 , Schraalhans Keukenmeester
<Schraalh...@th e.spamtrapexamp le.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*******@attgl obal.net
=============== ===
Jun 2 '07 #9
howa wrote:
On Jun 2, 7:51 pm, gosha bine <stereof...@gma il.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
13910
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it would be more orthogonal if a method could only have one return statement. --
7
1538
by: pmatos | last post by:
Hi all, I've been having questions about strings, references, initializations... I've created code (which will not compile due to a reference problem) and I'd like comments on why this won't work and any other comment to the code with efficiency in mind. #include <iostream> #include <string>
66
5417
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
1
1554
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: public class UnorderedLinkedList extends LinkedListClass { public UnorderedLinkedList()
7
1878
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: public class UnorderedLinkedList extends LinkedListClass { public UnorderedLinkedList()
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.