2013-03-18 98 views
1

我是面向对象的新手。我曾在程序编程方面做过很多工作。所以我现在有点麻烦了。你能告诉我如何在另一个类中调用一个类的对象,然后我可以使用该对象访问该类的所有变量和函数。在另一个类中调用一个类的对象

例如

我有一类DB连接。我写我的数据库查询。现在我有一个名为Users的其他类。现在我需要访问User类中的db查询,当然我需要DBconnection类的对象来访问所有db查询。我能做些什么,请帮助

我所编写的示例代码如下:

**DBConnection class** 

class DBConnection 
    { 
     public $SITEURL; 
     public $servername; 
     public $username; 
     public $password; 
     public $dbname; 
     public $objDbConnect; 

     function DBConnection(){ 
      $this->SITEURL=Configuration::SITEURL; 
      $this->servername=Configuration::servername; 
      $this->username=Configuration::username; 
      $this->password=Configuration::password; 
      $this->objDbConnect=mysql_connect($this->servername,$this->username,$this->password); 
       if($this->objDbConnect){ 
        mysql_select_db($this->dbname); 
       } 
      } 
    function InsertRecord($pStrTableName,$pArrField,$pArrValue) 
    { 

    $strSql="insert into $pStrTableName ("; 

    $intFieldSize=count($pArrField); 
    for($i=0;$i<$intFieldSize;$i++) 
    { 
     if($i==$intFieldSize-1) 
     { 
      $strSql.=$pArrField[$i]; 
     } 
     else 
     { 
      $strSql.=$pArrField[$i].","; 
     } 
    } 
    $strSql.=") values ("; 

    $intFieldSize=count($pArrValue); 
    for($i=0;$i<$intFieldSize;$i++) 
    { 
     if($i==$intFieldSize-1) 
     { 
      $strSql.="'".$pArrValue[$i]."'"; 
     } 
     else 
     { 
      $strSql.="'".$pArrValue[$i]."'".","; 
     } 
    } 
    $strSql.=")"; 
    if(mysql_query($strSql)) 
    { 
     return 1; 
    } 
    else 
    { 
     return 0; 
    } 

} 

} 

**Users class** 

class Users{ 

     var $username, 
      $userpassword, 
      $email, 


     function User(){ 

     } 


     } 

回答

0

做到这一点的最好方法是使用Singleton模式。 以下是一个示例

class DBConnection 
{ 
    private static $instance = null; 

    private function __construct() { 

    } 

    /* 
    * @return DBConnection 
    */ 
    public static function get_db() 
    { 
     if (empty(self::$instance)) self::$instance = new DBConnection(); 

     return self::$instance; 
    } 

    public function query() 
    { 

    } 
} 


class User 
{ 

    function testfunc() 
    { 
     $db = DBConnection::get_db(); 
     $db->query(); 
    } 
} 
0

您可以在第二类中声明你的类的对象,并使用它像

$c= new DBConnection(); 
$c->InsertRecord('Parameters here etc'); 
echo $c->username; //and other public variables similarly 
+0

我可以在DBConnection类中声明$ db = new DBConnection。所以如果我在Users类中包含DBConnection类,它将无法访问函数和变量。 – user2009243 2013-03-18 05:26:12

+0

为什么不能,你可以与我们分享一些不起作用的代码 – 2013-03-18 05:26:49

0

DBConnection的类

类DBConnection的 {

public $SITEURL; 
    public $servername; 
    public $username; 
    public $password; 
    public $dbname; 
    public $objDbConnect; 

    function DBConnection(){ 
     $this->SITEURL=Configuration::SITEURL; 
     $this->servername=Configuration::servername; 
     $this->username=Configuration::username; 
     $this->password=Configuration::password; 
     $this->objDbConnect=mysql_connect($this->servername,$this->username,$this->password); 
      if($this->objDbConnect){ 
       mysql_select_db($this->dbname); 
      } 
     } 
function InsertRecord($pStrTableName,$pArrField,$pArrValue) 
{ 

$strSql="insert into $pStrTableName ("; 

$intFieldSize=count($pArrField); 
for($i=0;$i<$intFieldSize;$i++) 
{ 
    if($i==$intFieldSize-1) 
    { 
     $strSql.=$pArrField[$i]; 
    } 
    else 
    { 
     $strSql.=$pArrField[$i].","; 
    } 
} 
$strSql.=") values ("; 

$intFieldSize=count($pArrValue); 
for($i=0;$i<$intFieldSize;$i++) 
{ 
    if($i==$intFieldSize-1) 
    { 
     $strSql.="'".$pArrValue[$i]."'"; 
    } 
    else 
    { 
     $strSql.="'".$pArrValue[$i]."'".","; 
    } 
} 
$strSql.=")"; 
if(mysql_query($strSql)) 
{ 
    return 1; 
} 
else 
{ 
    return 0; 
} 

}

}

用户类

类用户{

var $username; 
    var $userpassword; 
    var $email; 
    var $dbcon; 

public function __construct() 
{ 
    //create an object 
    $this->dbconn = new DBConnection(); 

    //get user name from dbconnection class 
    echo $this->dbconn->username; 

} 

    function User(){ 

    } 

}

//创建用于用户类对象

$ user = new User();

0
**DBConnection class** 

class DBConnection 
    { 
     public $SITEURL; 
     public $servername; 
     public $username; 
     public $password; 
     public $dbname; 
     public $objDbConnect; 

     function DBConnection(){ 
      $this->SITEURL=Configuration::SITEURL; 
      $this->servername=Configuration::servername; 
      $this->username=Configuration::username; 
      $this->password=Configuration::password; 
      $this->objDbConnect=mysql_connect($this->servername,$this->username,$this->password); 
       if($this->objDbConnect){ 
        mysql_select_db($this->dbname); 
       } 
      } 

    public static function getConnection(){ 
     if(!empty($this)){ 
     return $this; 
     } 
     return new DBConnection(); 
    } 


    function InsertRecord($pStrTableName,$pArrField,$pArrValue) 
    { 

    $strSql="insert into $pStrTableName ("; 

    $intFieldSize=count($pArrField); 
    for($i=0;$i<$intFieldSize;$i++) 
    { 
     if($i==$intFieldSize-1) 
     { 
      $strSql.=$pArrField[$i]; 
     } 
     else 
     { 
      $strSql.=$pArrField[$i].","; 
     } 
    } 
    $strSql.=") values ("; 

    $intFieldSize=count($pArrValue); 
    for($i=0;$i<$intFieldSize;$i++) 
    { 
     if($i==$intFieldSize-1) 
     { 
      $strSql.="'".$pArrValue[$i]."'"; 
     } 
     else 
     { 
      $strSql.="'".$pArrValue[$i]."'".","; 
     } 
    } 
    $strSql.=")"; 
    if(mysql_query($strSql)) 
    { 
     return 1; 
    } 
    else 
    { 
     return 0; 
    } 

} 

} 

**Users class** 

class Users{ 

     var $username, 
      $userpassword, 
      $email, 


     function User(){ 
      $db = DBConnection::getConnection(); 
      $db->InsertRecord($x, $x, $x); 
     } 


     } 
0

1。您可以将您的班级DBConnection更改为singleton。并在用户中致电您的课程,如DBConnection::getInstance()->InsertRecord(...)。这很容易。但不推荐。

2。你可以在你的User内推送你的DBConnection实例。像这样

class Users{ 
    var $db_connection; 

    function __construct($db_connection){ 
     $this->db_connection = $db_connection; 
    } 

$db_connection = new DBConnection(...); 
$user = new User($db_connection); 

3。您可以在User类中实例化新的DBCOnnection。就像在@Sundar答案中一样。

P.S.请勿使用old construct style

0

您可以在您的User类中保留对DBConnection的引用(将其传入构造函数中)。

private $dbConn; 

function __contruct($dbConnRef) { 
    $this->dbConn = $dbConnRef; 
} 

public function storeSelf() { 
    $this->dbConn->InsertRecord(...); 
}