集成类的最佳方法 [英] Best way to integrate classes

查看:75
本文介绍了集成类的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我在这里的一个小项目工作。我不是编程新手,但

我是PHP的新手。你必须明白我来自C ++,OOP世界,

所以我的代码可能看起来有点对象 - 确认。


无论如何,我已经为MySQL连接创建了一个包装类。现在它是准系统

,没有错误检查或任何东西。所以现在我正在尝试创建

用户/会话处理类,它将通过数据库类处理存储在

数据库中的数据。


集成这两个类的最佳方法是什么?你会怎么做?


我看到三种可能的解决方案:

1.)Session类是否扩展了DB类?

class Session extends DB {

// login();

// logout();

//等... < br $>
}


在逻辑继承方面,这对我来说真的没有意义。会话

实际上不是一个DB处理类。此外,我不清楚这里的一些事情

。是否会自动调用DB类的构造函数并创建一个

连接(它应该在我的脑海中)或者我应该重新初始化所有

继承的变量?

2.)首先,实例化DB的对象:

$ oDB = new DB();


然后通过全局声明使用它在Session类中


类Session {

global $ oDB;


// login();

// logout();

//等等......

}


不是最漂亮的方式,但它的工作原理。它还允许在代码中通过$ oDB对象重用相同的

MySQL连接。

3.)将Session对象实例化为Session'的vars的一部分:


班级会话{

$ _oDB =新DB();


//登录();

// logout();

//等等......

}


唯一我可以看到的缺点是我无法在Session类之外重用MySQL

连接。我将不得不创建一个全新的数据库

对象。


哦,这是数据库类。

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++ b $ b class DB {


//公共变量

var $ sServer;

var $ sPort;

var $ sUser;

var $ sPass;

var $ sDatabase;


//私有变量

var $ _link_id;

var $ _result_id;

var $ _last_query;


//构造函数,使用db_connect()

函数DB($ user,$ pass,$ database,$ server =''localhost'',$ port ='''3306''){

$ this-> connect($ user,$ pass,$ database,$ server,$ port);

}


//打开连接到db-server并选择一个db

函数connect($ user,$ pass,$ database,$ servet,$ port){

$ this-> sServer = $ server;

$ this-> sPort = $ port;

$ this-> sUser = $ user;

$ this - > sPass = $ pass;

$ this-> sDatabase = $ database;


$ this-> _link_id = mysql_connect(" $ server:$ port",$ user,$ pass);


$ this-> select_db($ database,$ this-> _link_id);

}


//关闭与数据库的连接

function disconnect(){

$ this-> free_result();

mysql_close($ this-> _link_id);

unset($ this-> _link_id,$ this-> _result_id);

}


//更改正在使用的数据库

函数select_db($ database){

mysql_select_db($ this-> sDatabase, $ this-> _link_id);

}


//运行db query

函数查询($ query){

$ this-> _last_query = $ query;

$ this-> _result_id = mysql_query($ query,$ this-> _link_id);

}


//释放存储上次查询结果所需的内存

函数free_result(){

mysql_free_r esult($ this-> _result_id);

未设置($ this-> _result_id);

}


/ /从查询结果返回单个记录数组

函数fetch_row(){

返回mysql_fetch_row($ this-> _result_id);

} $ / $

//从查询结果中返回单个记录作为关联数组

函数fetch_assoc(){

返回mysql_fetch_assoc($ this-> _result_id);

}


//从查询结果中返回单个记录作为对象

function fetch_object( ){

返回mysql_fetch_object($ this-> _result_id);

}


//返回之后找到的记录数SELECT语句

函数num_rows(){

返回mysql_num_rows($ this-> _result_id);

}

//返回受INSERT,UPDATE,DELETE语句影响的行数

函数affected_rows(){

返回mysql_affected_rows($ this-> _l ink_id);

}


//返回当前数据库link_id

函数get_link_id(){

返回$ this-> _link_id;

}


//返回当前数据库result_id

函数get_result_id(){

返回$ this-> _result_id;

}


//返回上次查询运行

function get_last_query(){

返回$ this-> _last_query;

}

// ---------- -------------------------------------------------- -----------

}


欢迎提出任何意见,也许我还有其他办法可以做到这一点'不是

看到了。


-

Swartz

Hi all.

I''m working here on a small project of mine. I''m not new to programming, but
I''m new to PHP. You have to understand that I''m coming from C++, OOP world,
so my code might seems a little too "object"-ified.

Anyways, I''ve created a wrapper class for MySQL connectivity. It''s barebones
for now, no error checking or anything. So now I''m trying to create
user/session-handling class that would work with the data stored in the
database via DB class.

What is the best approach to integrate the two classes? How would you do it?

I see three possible solutions:
1.) Have the Session class extend the DB class?
class Session extends DB {
// login();
// logout();
// etc...
}

This doesn''t really make sense to me in terms of logical iheritance. Session
is not really a DB-handling class. Besides, I''m unclear on a few things
here. Will the constructor for DB class be called automatically and create a
connection (it should in my mind) or should I be re-initializing all
inherited vars?
2.) First, instantiate an object of DB:
$oDB = new DB();

Then use it via global declaration in Session class

class Session {
global $oDB;

// login();
// logout();
// etc...
}

Not the prettiest way, but it works. It would also allow to reuse the same
MySQL connection via $oDB object further in the code.
3.) Instantiate DB object as part of Session''s vars:

class Session {
$_oDB = new DB();

// login();
// logout();
// etc...
}

The only drawback I can see is that I would be unable to reuse the MySQL
connection outside of Session class. I''d have to create a totaly new DB
object.

Oh and here''s the DB class.

// +++++++++++++++++++ DB CLASS +++++++++++++++++
class DB {

// public variables
var $sServer;
var $sPort;
var $sUser;
var $sPass;
var $sDatabase;

// private variables
var $_link_id;
var $_result_id;
var $_last_query;

// Constructor, uses db_connect()
function DB ($user, $pass, $database, $server=''localhost'', $port=''3306'') {
$this->connect($user, $pass, $database, $server, $port);
}

// Opens a connection to a db-server and selects a db
function connect ($user, $pass, $database, $servet, $port) {
$this->sServer = $server;
$this->sPort = $port;
$this->sUser = $user;
$this->sPass = $pass;
$this->sDatabase = $database;

$this->_link_id = mysql_connect("$server:$port", $user, $pass);

$this->select_db($database, $this->_link_id);
}

// Closes connection to the database
function disconnect () {
$this->free_result();
mysql_close($this->_link_id);
unset ($this->_link_id, $this->_result_id);
}

// Changes database in use
function select_db ($database) {
mysql_select_db($this->sDatabase, $this->_link_id);
}

// Run db query
function query ($query) {
$this->_last_query = $query;
$this->_result_id = mysql_query($query, $this->_link_id);
}

// Frees up the memory required to store last query''s results
function free_result () {
mysql_free_result($this->_result_id);
unset ($this->_result_id);
}

// Returns a single record array from the query results
function fetch_row () {
return mysql_fetch_row($this->_result_id);
}

// Returns a single record as associative array from query results
function fetch_assoc () {
return mysql_fetch_assoc($this->_result_id);
}

// Returns a single record as object from query results
function fetch_object () {
return mysql_fetch_object($this->_result_id);
}

// Returns number of records found after SELECT statement
function num_rows () {
return mysql_num_rows($this->_result_id);
}

// Return number of rows affected from INSERT, UPDATE, DELETE statements
function affected_rows () {
return mysql_affected_rows($this->_link_id);
}

// Returns current db link_id
function get_link_id () {
return $this->_link_id;
}

// Return current db result_id
function get_result_id () {
return $this->_result_id;
}

// Return last query ran
function get_last_query () {
return $this->_last_query;
}
// -----------------------------------------------------------------------
}

Any comments are welcome, perhaps there''re other way to do this that I''m not
seeing.

--
Swartz

推荐答案

oDB = new DB();


然后在Session类中通过全局声明使用它

class Session {

global
oDB = new DB();

Then use it via global declaration in Session class

class Session {
global


oDB;


//登录();

// logout();

//等等。 。

}


不是最漂亮的方式,但它有效。它还允许在代码中通过
oDB;

// login();
// logout();
// etc...
}

Not the prettiest way, but it works. It would also allow to reuse the same
MySQL connection via


oDB对象重用相同的

MySQL连接。

3.)实例化数据库对象作为Session'vars的一部分:


class Session {
oDB object further in the code.
3.) Instantiate DB object as part of Session''s vars:

class Session {


这篇关于集成类的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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