View Single Post

  #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