- > PHP4 Singleton实现问题< - [英] -> PHP4 Singleton implementation question <-

查看:73
本文介绍了 - > PHP4 Singleton实现问题< - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在PHP4中实现一个单例,但它似乎并没有
工作。每次调用它时都会重新创建对象。


该类的目标是使变量保持最新。

它已经习惯了显示数据库内容,一次25行。

单例跟踪当前起始行和

将其增加或减少25,具体取决于用户操作

(按下一个或上一个按钮)。

这些按钮是提交按钮,调用当前表单本身。


但每次我重新输入那个表格,再次创建单例变量

,因此重新获得currentRow变量。


感谢你的帮助吗?


以下是名为Welcome的类的代码:

------------------------- --------------------

class欢迎{

var $ offsetRows = 25;

var $ currentRow;

// ********************************* ***************** ********

// INSTANCE函数用于实现此类只有一次

// ************************************** ************ ********

function& getInstance(){

static $ instance;

if(!$ instance){

$ instance = new Welcome();

}

返回$ instance; < br $>
}


// **************************** ********************** ********

//创建对象时调用的CONSTRUCT函数

// ******************************************* ******* ********

功能欢迎(){

$ this-> currentRow = 0;

$ this-> offsetRows = 25;

}

// ******************* ******************************* ********

// SHOWRECORDS

//显示包含行信息的实际表格。

// ********************* ***************************** ********

function showRecords(){

//我的表显示代码在这里使用$ this-> currentRow

}


// ************************************************** ********

// NEXTRECORDS

//显示下一个nn偏移记录

// ***** ********************************************* ***** ***

函数nextRecords(){

$ this-> currentRow + = $ this-> offsetRows;

$ this- > showRecords();

}

// ************************* ************************* ********

// PREVRECORDS

//如果不是,则显示先前的nn偏移记录

// ************************** ************************ ********

功能prevRecords(){

$ this-> currentRow - = $ this-> offsetRows;

if($ this-> currentRows< 0)

$ this-> currentRows = 0;

$ this-> showRecords();

}

}

然后我的表格如下:

----------------------- -------

< FORM action ="<?= $ _ SERVER [''PHP_SELF'']?>" method =" post">

<?php

require_once(" class_welcome.php");

if(!$欢迎){

$ welcome =& Welcome :: getInstance();

}


if(isset($ _ POST [''next''])){

$ welcome-> nextRecords();

}

else {

if(isset($ _ POST [''previous''' ])){

$ welcome-> prevRecords();

}

else {

$ welcome - > showRecords();

}

}

?>


< P>

< INPUT name =" previous"类型= [提交"值= QUOT;<<" />

< INPUT name =" next"类型= [提交"值= QUOT;>>" />

< / FORM>


此致,

Steve JORDI


(从我的电子邮件地址中删除K_I_L_LSPAM)

--------------------------- ---------------------

1197 Prangins电子邮件: st ******************* @ hotmail.com

瑞士WWW: www.sjordi.com

- ----------------------------------------------

火山在 www.sjordi.com/volcanoes

MovieDB在 www.sjmoviedb.com

- ----------------------------------------------

解决方案

offsetRows = 25;

var


currentRow;

// ***************************************** ********* ********

// INSTANCE函数只能将此类安装一次

// ***** ********************************************* ***** ***

function& getInstance(){

static


instance;

if( !

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: st*******************@hotmail.com
Switzerland WWW: www.sjordi.com
------------------------------------------------
Volcanoes at www.sjordi.com/volcanoes
MovieDB at www.sjmoviedb.com
------------------------------------------------

解决方案

offsetRows = 25;
var


currentRow ;
// ************************************************** ********
// INSTANCE function to instanciate this class only once
// ************************************************** ********
function &getInstance() {
static


instance ;
if( !


这篇关于 - &GT; PHP4 Singleton实现问题&lt; - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆