-> PHP4 Singleton implementation question <-

This is a discussion on -> PHP4 Singleton implementation question <- within the PHP Language forums, part of the PHP Programming Forums category; Hi, I'm trying to implement a singleton in PHP4 but it doesn't seem to work. The object is ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-30-2006
Steve JORDI
 
Posts: n/a
Default -> PHP4 Singleton implementation question <-

Hi,
I'm trying to implement a singleton in PHP4 but it doesn't seem to
work. The object is recreated each time I call it.

The goal of the class is to keep a variable up to date.
It's used to display a database content, 25 rows at a time.
The singleton keeps track of the current starting row and
increases it or decreases it by 25 depending on the user action
(pressing a Next or Prev button).
Those buttons are submit buttons calling the current form itself.

But each time I re-enter that form, the singleton variable is created
again and therefore the currentRow variable reinitilized.

Thanks for any help?

Here is the code for the class called Welcome:
---------------------------------------------
class Welcome {
var $offsetRows = 25;
var $currentRow ;


// ************************************************** ********
// INSTANCE function to instanciate this class only once
// ************************************************** ********
function &getInstance() {
static $instance ;
if( !$instance ) {
$instance = new Welcome() ;
}
return $instance ;
}

// ************************************************** ********
// CONSTRUCT function called when object is created
// ************************************************** ********
function Welcome() {
$this->currentRow = 0 ;
$this->offsetRows = 25 ;
}


// ************************************************** ********
// SHOWRECORDS
// Displays the actual table with info in rows.
// ************************************************** ********
function showRecords() {
// my table display code here using $this->currentRow
}

// ************************************************** ********
// NEXTRECORDS
// Displays the next nn offset records
// ************************************************** ********
function nextRecords() {
$this->currentRow += $this->offsetRows ;
$this->showRecords() ;
}


// ************************************************** ********
// PREVRECORDS
// Displays the previous nn offset records if not at first
// ************************************************** ********
function prevRecords() {
$this->currentRow -= $this->offsetRows ;
if( $this->currentRows < 0 )
$this->currentRows = 0 ;
$this->showRecords() ;
}
}


Then my form works as follows:
------------------------------
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php
require_once( "class_welcome.php" ) ;
if( !$welcome ) {
$welcome =& Welcome::getInstance() ;
}

if(isset($_POST['next'])) {
$welcome->nextRecords() ;
}
else {
if(isset($_POST['previous'])) {
$welcome->prevRecords() ;
}
else {
$welcome->showRecords() ;
}
}
?>

<P>
<INPUT name="previous" type="submit" value="<<" />
<INPUT name="next" type="submit" value=">>" />
</FORM>




Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
  #2 (permalink)  
Old 11-30-2006
Kimmo Laine
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

"Steve JORDI" <steveK_I_L_LSPAMjordi@hotmail.com> wrote in message
news:o08tm2to3vt8bkkka03mqot1dbo2t83jhc@4ax.com...
> Hi,
> I'm trying to implement a singleton in PHP4 but it doesn't seem to
> work. The object is recreated each time I call it.


PHP4 is not the best platform for OOP. PHP5 has much more sophisticated
object handling.

> The goal of the class is to keep a variable up to date.
> It's used to display a database content, 25 rows at a time.
> The singleton keeps track of the current starting row and
> increases it or decreases it by 25 depending on the user action
> (pressing a Next or Prev button).
> Those buttons are submit buttons calling the current form itself.
>
> But each time I re-enter that form, the singleton variable is created
> again and therefore the currentRow variable reinitilize


I'm guessing you have a C++ or Java background. The request/response
architecture is quite different from a desktop application. A variable will
not be available thru two different page calls unless it's stored as a
session variable. Otherwise, each page call should be concidered as
restarting an application and at the end of the page all variables are
destroyed except for session variables.

First I encourage you to go for php version 5, you'll get much more out of
the classes and objects. Next thing you should do is learn how sessions
work. And then finally, check out the singleton example at php.net:
http://www.php.net/manual/en/language.oop5.patterns.php

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)


Reply With Quote
  #3 (permalink)  
Old 11-30-2006
Steve JORDI
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

Kimmo,

Thanks for your prompt reply,


>PHP4 is not the best platform for OOP. PHP5 has much more sophisticated
>object handling.


Yes I know but for now I have no choice but to use the existing PHP 4
in the company.

>I'm guessing you have a C++


Correct :-)


>The request/response architecture is quite different from a desktop
>application. A variable will not be available thru two different
>page calls unless it's stored as a session variable.


Mhhh... Interresting.

>Otherwise, each page call should be concidered as restarting an
>application and at the end of the page all variables are
>destroyed except for session variables.


OK.


>First I encourage you to go for php version 5, you'll get much more out of
>the classes and objects. Next thing you should do is learn how sessions
>work.


Ok I will check session matters. Thanks for the hint.

>And then finally, check out the singleton example at php.net:
>http://www.php.net/manual/en/language.oop5.patterns.php


Yes, I've seen it, but it's for PHP 5 which I can't use right now.

Thanks for your help.

Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
  #4 (permalink)  
Old 11-30-2006
Dikkie Dik
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

<snip>
> Then my form works as follows:
> ------------------------------
> <FORM action="<?=$_SERVER['PHP_SELF']?>" method="post">
> <?php
> require_once( "class_welcome.php" ) ;
> if( !$welcome ) {
> $welcome =& Welcome::getInstance() ;
> }


Following the PHP documentation on http://nl3.php.net/static , I would
suggest to replace this last line with a non-referencing one:

$welcome = Welcome::getInstance() ;

As was posted earlier, PHP5 has solved the objects-and-references problem.

Best regards.
Reply With Quote
  #5 (permalink)  
Old 11-30-2006
Steve JORDI
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

>Following the PHP documentation on http://nl3.php.net/static , I would
>suggest to replace this last line with a non-referencing one:
>$welcome = Welcome::getInstance() ;


Did this but it din't change a thing.

>As was posted earlier, PHP5 has solved the objects-and-references problem.


Yes, but I have to deal with PHP4, not 5 unfortunately.

I also tried to set a $_SESSION['welcome'] variable, but still, each
time I reenter my page, it's reassigned a new instance.

It's crazy, I would never have thought that it would be such a hassle
to keep a variable value between pages (without using URL parameters).

Thanks anyway.

Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
  #6 (permalink)  
Old 11-30-2006
Dikkie Dik
 
Posts: n/a
Default session (was: -> PHP4 Singleton implementation question <-)

<snip>
> I also tried to set a $_SESSION['welcome'] variable, but still, each
> time I reenter my page, it's reassigned a new instance.
>
> It's crazy, I would never have thought that it would be such a hassle
> to keep a variable value between pages (without using URL parameters).



Sorry, I did not correctly read your message, and I thought it was
re-created within one page request. If you want something kept between
page requests, you'll need a session. There's no need to store the
object itself in the session, although that is not impossible. Even
then, the object will be re-created (unserialized) for each page visit.
You can store just the data itself in the session (I assume it is one or
more arrays) and have your instance check for existence of that session
data at instantiation. Be sure to start the session before any data is
sent to the client by calling session_start(). See
http://nl3.php.net/manual/en/function.session-start.php for more info.

Best regards.
Reply With Quote
  #7 (permalink)  
Old 11-30-2006
Jerry Stuckle
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

Steve JORDI wrote:
>>Following the PHP documentation on http://nl3.php.net/static , I would
>>suggest to replace this last line with a non-referencing one:
>>$welcome = Welcome::getInstance() ;

>
>
> Did this but it din't change a thing.
>
>
>>As was posted earlier, PHP5 has solved the objects-and-references problem.

>
>
> Yes, but I have to deal with PHP4, not 5 unfortunately.
>
> I also tried to set a $_SESSION['welcome'] variable, but still, each
> time I reenter my page, it's reassigned a new instance.
>
> It's crazy, I would never have thought that it would be such a hassle
> to keep a variable value between pages (without using URL parameters).
>
> Thanks anyway.
>
> Sincerely,
> Steve JORDI
>
> (Remove the K_I_L_LSPAM from my email address)
> ------------------------------------------------
> 1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
> Switzerland WWW: www.sjordi.com
> ------------------------------------------------
> Volcanoes at www.sjordi.com/volcanoes
> MovieDB at www.sjmoviedb.com
> ------------------------------------------------


Steve,

If you've ever done any transactional processing, that's what web pages are.

When the browser makes a request, the server starts a process (or
thread) to handle the request. It allocates the necessary resources,
and turns control over to your program (the php interpreter, in this
case). The interpreter allocates additional resources as necessary to
process your script and runs the script.

When the script ends, the process reverses. The interpreter cleans up
its resources and returns to the server. The server then cleans up the
resources it allocated and terminates the process or thread.

Each request is separate in itself. Resources are not kept, because
another request may or may not follow this one. And if another request
does come in, it may or may not be something you expect. And it may or
may not be from the same user.

Now, the developers understood there was a need to save information
across requests. Therefore they implemented sessions to save the data
on the server, and cookies to save it on the browser.

So, if you want to save your object across the request, you need to
store it in the session (not a good idea to store this in the cookie -
too many people have cookies disabled, and saving them on the user's
computer allows the user to edit the cookie).


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #8 (permalink)  
Old 12-01-2006
Steve JORDI
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

Jerry,
thanks for your explanations.

I did actually try to save the variable as a session one, but
it didn't seem to work.

I had a
start_session() ;

Then, when entering or re-entering my page, it tests
if( !isset($_SESSION['welcome']) )
$_SESSION['welcome'] =& Welcome::GetInstance() ;

Problem is that it correctly get the instance the first time,
but gets it again each time I re-enter my page.

Shouldn't $_SESSION['welcome'] be saved within the session?

From the literrature I read, it's the way to go, but clearly,
something's wrong, I'm missing something.



Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
  #9 (permalink)  
Old 12-01-2006
Jerry Stuckle
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

Steve JORDI wrote:
> Jerry,
> thanks for your explanations.
>
> I did actually try to save the variable as a session one, but
> it didn't seem to work.
>
> I had a
> start_session() ;
>
> Then, when entering or re-entering my page, it tests
> if( !isset($_SESSION['welcome']) )
> $_SESSION['welcome'] =& Welcome::GetInstance() ;
>
> Problem is that it correctly get the instance the first time,
> but gets it again each time I re-enter my page.
>
> Shouldn't $_SESSION['welcome'] be saved within the session?
>
> From the literrature I read, it's the way to go, but clearly,
> something's wrong, I'm missing something.
>
>
>
> Sincerely,
> Steve JORDI
>
> (Remove the K_I_L_LSPAM from my email address)
> ------------------------------------------------
> 1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
> Switzerland WWW: www.sjordi.com
> ------------------------------------------------
> Volcanoes at www.sjordi.com/volcanoes
> MovieDB at www.sjmoviedb.com
> ------------------------------------------------


Yes, and if your session is working, you it will be. Are you sure
NOTHING is sent to the browser before the session_start() call? No
white space, no DOCTYPE, nothing?

Enable all errors on the page by adding this at the top of your PHP code:

ini_set("display_errors","1");
error_reporting(E_ALL);

And see what you get.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #10 (permalink)  
Old 12-01-2006
Steve JORDI
 
Posts: n/a
Default Re: -> PHP4 Singleton implementation question <-

OK I will check everything on Monday when back to work.
I'll keep you posted.
Thanks for your help anyway.

Sincerely,
Steve JORDI

(Remove the K_I_L_LSPAM from my email address)
------------------------------------------------
1197 Prangins Email: stevejordiK_I_L_LSPAM@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:49 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0