You are here: Re: -> PHP4 Singleton implementation question <- « PHP Programming Language « IT news, forums, messages
Re: -> PHP4 Singleton implementation question <-

Posted by Steve JORDI on 12/04/06 13:41

Jerry,
I include the class definition before the start session
the PHP.INI is not set to start sessions automatically.

Maybe some code excerpts would help...


Here is the index.php start file:
--------------------------------
<?php
include_once( "class_welcome.php" ) ;
session_start() ;
ini_set("display_errors","1");
error_reporting(E_ALL);
header("Location: ./passeportswelcome.php") ;
?>



then I have passeportswelcome.php:
----------------------------------
<body>
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="post">
<?php

if( !isset( $_SESSION['welcome'] ) )
$_SESSION['welcome'] =& Welcome::getInstance() ;

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

?>

<P>
<INPUT name="previous" type="submit" value="&lt;&lt;" />
<INPUT name="next" type="submit" value="&gt;&gt;" />
</FORM>
</body>



and finally, here is the begining of the class_welcome.php:
-----------------------------------------------------------
<?php
// **********************************************************
// CLASS WELCOME
// Displays records in a table
// Limited to nn records with nn chunks offsets
//
// This is a SINGLETON
// Use it in your main form as $obj = &Welcome::getInstance() ;
// **********************************************************
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 ;
}

// **********************************************************
// CONSTRUCTOR 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() {
// display code
}

// **********************************************************
// NEXTRECORDS
// **********************************************************
function nextRecords() {
$this->currentRow += $this->offsetRows ;
$this->showRecords() ;
}
}
?>


What it does is correctly display my table from rows 0 to 25 going
through (I've added "echo" to each function)
***WELCOME INSTANCE INIT***
***WELCOME CONSTRUCTOR***
***WELCOME INSTANCE ***
***SHOW RECORDS***

Then if I click on the ">>" button (being "next" in the form), it
goes through
***WELCOME INSTANCE INIT***
***WELCOME CONSTRUCTOR***
***WELCOME INSTANCE ***
***NEXT RECORDS***
***SHOW RECORDS***

As you can see, it instances Welceom again and goes again through
the constructor which initializes $currentRow to 0 again...
It should skip "Instance init" and "constructor".

Thanks



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
------------------------------------------------

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация