Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP,如何从另一个文件接收已连接的db,以便抛出到每个函数_Php - Fatal编程技术网

PHP,如何从另一个文件接收已连接的db,以便抛出到每个函数

PHP,如何从另一个文件接收已连接的db,以便抛出到每个函数,php,Php,我有2个文件(类),connect_db.php和manage.php,我想从connect_db类拉db connected链接到manage类 但是现在,我仍然在每个函数中使用相同的连接接收,所以我只想在这个类中使用一次,然后抛出到 用于每个函数。我听说过Getter和Setter的概念,但我从来没有使用过,也不确定它是否适合 这个案子 <?php //**connect_db.php** class connect_db { public function connect_d

我有2个文件(类),connect_db.php和manage.php,我想从connect_db类拉db connected链接到manage类 但是现在,我仍然在每个函数中使用相同的连接接收,所以我只想在这个类中使用一次,然后抛出到 用于每个函数。我听说过Getter和Setter的概念,但我从来没有使用过,也不确定它是否适合 这个案子

<?php

//**connect_db.php**

class connect_db
{
  public function connect_db()
  {
      $link = new PDO("mysql:host=127.0.0.1;dbname=net_pracharat","root","");
      $link -> exec("set names utf8");

      return $link;
  }
}  

?>




<?php
//**mange_db.php**

include("connect_db.php");

class mange
{

  //How I can do set here and send to each function

   public function view_process()
   {

      $link1 = new connect_db(); //**************************
      $link = $link1 -> connect_db(); //**************************
      //$link = $this->connect_db();

      $test_view = $link->prepare("SELECT *
                                   FROM tbl_province");
        $test_view -> execute();
        $result = $test_view->fetchAll(PDO::FETCH_ASSOC);

        return $result;
   }



   public function add_process()
   {
      $link1 = new connect_db(); //**************************
      $link = $link1 -> connect_db(); //**************************

      /*
        Process
      */

   }

   public function edit_process()
   {

      $link1 = new connect_db(); //**************************
      $link = $link1 -> connect_db(); //**************************

      /*
        Process
      */

   }

   public function delete_process()
   {
      $link1 = new connect_db(); //**************************
      $link = $link1 -> connect_db(); //**************************

      /*
        Process
      */
   }

}

?>

我看到很多选择

  • 构造函数中的初始化
  • 班级经理{ 私人$db; 公共函数构造(){ $connnect=new connect_db(); $this->db=$connnect->connect_db(); } 公共功能视图_进程(){ $this->db->prepare(); } }
  • 二传
  • 班级经理{ 私人$db; 公共函数setDb($db){ $this->db=$db; } 公共功能视图_进程(){ $this->db->prepare(); } }
  • 我最喜欢的:构造函数中静态方法的初始化
  • 类连接{ 公共静态函数connect_db(){ $link=newpdo(“mysql:host=127.0.0.1;dbname=net_pracharat”,“root”,“根”); $link->exec(“设置名称utf8”); 返回$link; } } 班级经理{ 私人$db; 公共函数构造(){ $this->db=connect::connect_db(); } 公共功能视图_进程(){ $this->db->prepare(); } } 当然,您也可以启动静态变量:

    class connect { static $db; public static function connect_db() { if (!self::$db) { $link = new PDO("mysql:host=127.0.0.1;dbname=net_pracharat", "root", ""); $link->exec("set names utf8"); self::$db = $link; } return self::$db; } } 类连接{ 静态$db; 公共静态函数connect_db(){ 如果(!self::$db){ $link=newpdo(“mysql:host=127.0.0.1;dbname=net_pracharat”,“root”,“根”); $link->exec(“设置名称utf8”); self::$db=$link; } 返回self::$db; } } 看:

    class mange { private $db; public function setDb($db) { $this->db = $db; } public function view_process() { $this->db->prepare(); } } class connect { public static function connect_db() { $link = new PDO("mysql:host=127.0.0.1;dbname=net_pracharat", "root", ""); $link->exec("set names utf8"); return $link; } } class mange { private $db; public function __construct() { $this->db = connect::connect_db(); } public function view_process() { $this->db->prepare(); } } class connect { static $db; public static function connect_db() { if (!self::$db) { $link = new PDO("mysql:host=127.0.0.1;dbname=net_pracharat", "root", ""); $link->exec("set names utf8"); self::$db = $link; } return self::$db; } }