簡體   English   中英

如何在php中包含所有函數的文件

[英]how can i include files for all functions in php

我是php的新手,因為你知道,我有一些問題(像任何新的開始),我的問題是我不能包括文件,如config.php文件中的所有功能

我只有3個文件

config.php我只是連接到mysqli中的數據庫,變量是$connect

在functions.php中只有一個簡單的函數

include('config.php');
function rows($table){
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

在index.php中

include('functions.php');
echo rows('books');

問題是我無法從config.php獲取$connect變量以在functions.php文件中的函數中functions.php

編輯

當我在函數中包含config.php ,一切都會好的,就像這樣

function rows($table){
include('config.php');
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

但我不想這樣做,因為我想為我擁有的所有功能都包含一個config.php

謝謝

$ config存在於'config.php'中。

在你的函數中,將$ connect聲明為'global'即:它存在於此函數的范圍之外。

function rows($table){

    global $connect;

    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

兩種可能的方式是可用的。

1.Passing $ connect作為函數的第二個參數

include('config.php');
function rows($table,$connnect){
    $gettables = mysqli_query($connect,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}
  1. 將$ connect聲明為全局變量

      function rows($table){ global $connect; $gettables = mysqli_query($connect,"SELECT * FROM $table"); $numrows = mysqli_num_rows($gettables); return($numrows); } 

建議將$ connect作為參數傳遞給函數。不推薦使用全局變量參考這里PHP中的全局變量被認為是不好的做法嗎? 如果是這樣,為什么?

問題是您在全局范圍內聲明了一個變量,但是在全局范圍內可用的任何內容都不會在函數范圍內自動提供,除非您通過global $connect強制它具有自己的警告。

global一些替代方案將是。

1)使用單例類:

<?php
//File connect.php
class Connection {
    private static $connection = null;
    public static get() {
          if (self::$connection == null) { self::$connection = mysqli_connect(...); }
          return self::$connection;
    }
} 

並用它作為

include_once('connect.php');
function rows($table){
    $gettables = mysqli_query(Connection::get(),"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

2)常數(不是最好的主意):

 <?php 
 //File config.php
 define('CONNECTION',mysqli_connect(...));

用於:

include_once('config.php');
function rows($table){
    $gettables = mysqli_query(CONNECTION,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

3)將它作為參數傳遞給函數(config.php就像你現在一樣):

function rows($connection, $table){
    $gettables = mysqli_query($connection,"SELECT * FROM $table");
    $numrows = mysqli_num_rows($gettables);
    return($numrows);
}

用於:

include('config.php');
include_once('functions.php');
echo rows($connection, 'books');

4)讓文件像變量聲明一樣工作

<?php
//config.php 
return mysqli_connect(...);

用於:

function rows($table){
     $connect = include('config.php');
     $gettables = mysqli_query($connect,"SELECT * FROM $table");
     $numrows = mysqli_num_rows($gettables);
     return($numrows);
}

你需要在你的函數中啟動一個新的對象配置,並使用constructer這樣的數據庫

$c=new config();
$con=$c->connect();
$req="SELECT * FROM table";
$conn=$con->query($req);

如果你在config.php中完成了一個類配置,這是有效的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM