0
我有2个文件(Class),connect_db.php和manage.php,我想从Connect_db类中拉db连接的链接到管理类 但现在我仍然把相同的连接接收放在每个功能中,所以我只想在这个类中放一次,并且为每个功能使用 。我听到了Getter和Setter的概念,但我从来没有使用过,也不确定它是否适合 这种情况。PHP,我怎样才能从另一个文件接收连接的数据库,以抛出每个函数
<?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
*/
}
}
?>