简体   繁体   English

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

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

i'm new in php as you know and i have some problems (like any new starts) and my problem is i cant include file like config.php to all functions in the files 我是php的新手,因为你知道,我有一些问题(像任何新的开始),我的问题是我不能包括文件,如config.php文件中的所有功能

i have just 3 files 我只有3个文件

in config.php i just connect to database in mysqli and the variable is $connect config.php我只是连接到mysqli中的数据库,变量是$connect

in functions.php there is just a simple functions 在functions.php中只有一个简单的函数

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

in index.php 在index.php中

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

and the problem is i cant get $connect variable from config.php to work in functions in functions.php file. 问题是我无法从config.php获取$connect变量以在functions.php文件中的函数中functions.php

EDIT 编辑

when i include config.php inside the functions everything will be ok, like this 当我在函数中包含config.php ,一切都会好的,就像这样

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

but i don't want to do this because i want to include a config.php for all functions i have. 但我不想这样做,因为我想为我拥有的所有功能都包含一个config.php

Thanks 谢谢

$connect exists in 'config.php'. $ config存在于'config.php'中。

In your function(s), declare $connect as 'global' ie: it exists outside the scope of this function. 在你的函数中,将$ connect声明为'global'即:它存在于此函数的范围之外。

function rows($table){

    global $connect;

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

Two possible ways are avaliable. 两种可能的方式是可用的。

1.Passing $connect as a second parameter to the function 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. Declaring $connect as a global variable 将$ connect声明为全局变量

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

It is recommended to pass $connect as a parameter to the function.Not recommended to use global variable refer here Are global variables in PHP considered bad practice? 建议将$ connect作为参数传递给函数。不推荐使用全局变量参考这里PHP中的全局变量被认为是不好的做法吗? If so, why? 如果是这样,为什么?

The problem is you declare a variable in the global scope, however whatever is available in the global scope is not automatically available in the function scope unless your force it available via global $connect which has its own caveats. 问题是您在全局范围内声明了一个变量,但是在全局范围内可用的任何内容都不会在函数范围内自动提供,除非您通过global $connect强制它具有自己的警告。

Some alternatives to global would be. global一些替代方案将是。

1) Use a singleton class: 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;
    }
} 

And use it as 并用它作为

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

2) Constants (Not the best idea): 2)常数(不是最好的主意):

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

Use as: 用于:

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

3) Pass it as a parameter to the function (config.php is as you have it now): 3)将它作为参数传递给函数(config.php就像你现在一样):

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

Use as: 用于:

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

4) Have the file work like a variable declaration 4)让文件像变量声明一样工作

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

Use as: 用于:

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

you need to initiate a new object config inside your function and use the constructer to conect to database like this 你需要在你的函数中启动一个新的对象配置,并使用constructer这样的数据库

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

this works if you have done a class config in config.php 如果你在config.php中完成了一个类配置,这是有效的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM