构造函数为“重置”按键 [英] Constructor as a "Reset" Button

查看:59
本文介绍了构造函数为“重置”按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在BLAST写这个应用程序作为OOP,因为PHP允许。讨论舞会是一件很好的事情,但看到

的表现真是太美了。


我注意到构造函数是重置的。开关 - 就像我电脑前面的

一样。调用它似乎只是将旧的值转储所有旧的值,释放所有旧资源,并将对象

返回到原始状态。


然而,我很少*担心只是使用构造函数

willy-nilly就像那样重置对象,因为,几年前,我

与其他程序员有一个问题,他说我没有做一些

DIE命令会导致僵尸进程。


这些问题似乎从来没有显现出来,但它仍然引起了原则,让我在烧烤架上了解它。


我是否为僵尸进程或其他资源浪费做好准备如果

我喜欢有一个db-connected类,然后让BANG重置它

构造函数。在这种特殊情况下,我并不担心程序员这样做是否好

,而是 - 只是说我不浪费任何东西

资源。


这是我用来连接数据库的代码(注意缺少使用

的DIE命令):


class bvckvs_database {

//所有属性都是只读的。

var $ Server;

var $ DatabaseName ;

var $用户名;

var $密码;

var $ TablePrefix;

var $ RecordSQL; // String

var $ RecordSet; //数组数组

var $ RecordCount; //整数

var $ ErrorMessage; //好或消息

var $ oConn; //数据库连接资源

函数bvckvs_database(){

$ this-> Server = _BVCKVSDBSERVER;

$ this-> DatabaseName = _BVCKVSDBDATABASE;

$ this-> Username = _BVCKVSDBUSERNAME;

$ this-> Password = _BVCKVSDBPASSWORD;

$ this-> TablePrefix = _BVCKVSUNIQUEPREFIX;

$ this-> RecordCount = 0;

$ this-> RecordSet = array();

$ this- > RecordSQL ="" ;;

$ this-> ErrorMessage =" OK";

$ bVoid = $ this-> Connect();

}

函数Connect(){

$ bRetVal = true;

if(!$ this-> oConn = @mysql_connect($ this-> Server,$ this-> Username,

$ this->密码)){

$ this-> ErrorMessage =无法连接到数据库服务器。;

$ bRetVal = false;

}否则{

if(!mysql_selectdb( $ this-> DatabaseName,$ this-> oConn)){

$ this-> ErrorMessage ="无法选择数据库。" ;;

$ bRetVal = false;

}否则{

$ this-> ErrorMessage =" OK" ;; < br $>
}

}

返回$ bRetVal;

}

I''m having a BLAST writing this app as OOP as PHP allows. It''s one
thing to discuss the dance, but it''s a thing of beauty to see the
performance.

I''m noticing that the constructor is a "reset" switch - just like the
one on the front of my computer. Calling it seems to just dump all of
the old values, free up all of the old resources, and return the object
to a pristine state.

However, I''m a *little* concerned about just using the constructor
willy-nilly like that to reset the object because, several years back, I
had an issue with other programmers who said that my failure to do some
"DIE" commands would result in zombie processes.

The problems never seemed to manifest themselves, but it still caused
the principles to get all up in my grill about it.

Am I setting myself up for zombie processes or other resource-wasting if
I like have a db-connected class and then just BANG reset it with the
constructor. In this particular case, I''m not concerned if "good"
programmers do it this way, but rather - just that I''m not wasting any
resources.

This is the code I use to connect to the database (note the lack of use
of a DIE command):

class bvckvs_database {
//All properties are read-only.
var $Server;
var $DatabaseName;
var $Username;
var $Password;
var $TablePrefix;
var $RecordSQL; // String
var $RecordSet; // Array of Arrays
var $RecordCount; // Integer
var $ErrorMessage; // OK or message
var $oConn; // Database Connection Resource
function bvckvs_database(){
$this->Server=_BVCKVSDBSERVER;
$this->DatabaseName=_BVCKVSDBDATABASE;
$this->Username=_BVCKVSDBUSERNAME;
$this->Password=_BVCKVSDBPASSWORD;
$this->TablePrefix = _BVCKVSUNIQUEPREFIX;
$this->RecordCount = 0;
$this->RecordSet = array();
$this->RecordSQL = "";
$this->ErrorMessage = "OK";
$bVoid = $this->Connect();
}
function Connect(){
$bRetVal = true;
if (!$this->oConn = @mysql_connect($this->Server, $this->Username,
$this->Password)) {
$this->ErrorMessage="Failed to connect to database server.";
$bRetVal = false;
} else {
if (!mysql_selectdb($this->DatabaseName,$this->oConn)) {
$this->ErrorMessage="Failed to select database.";
$bRetVal = false;
} else {
$this->ErrorMessage="OK";
}
}
return $bRetVal;
}

推荐答案

服务器;

var
Server;
var


DatabaseName;

var
DatabaseName;
var


用户名;

var
Username;
var


这篇关于构造函数为“重置”按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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