简体   繁体   English

如何访问函数内部的变量?

[英]How do I access a variable inside a function?

I am writing a php file, which will contain functions, of tasks I want to be executed. 我正在写一个PHP文件,其中包含要执行的任务的功能。 Some of these functions will need to use the mysql database. 其中一些功能将需要使用mysql数据库。 I have created a separate db_connections.php file which I have added to the functions file, using require_once. 我创建了一个单独的db_connections.php文件,已使用require_once将其添加到功能文件中。

I can access the PDO object created in this file, but I cannot access this variable inside my functions. 我可以访问在此文件中创建的PDO对象,但是无法在函数内部访问此变量。 Is there a way of accessing the variable inside functions? 有没有一种方法可以访问函数内部的变量?

<?php

require_once ("class.phpmailer.php"); 
require_once ("error_option.php"); 
require_once ("db_connection.php"); //The PDO object has been created here

function processLoginUser($email, $password) {
    //I cant seem to access the pdo object inside functions, but it is accessible outside functions
    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam();
}

You can pass the connection object when you call the function: 调用函数时可以传递连接对象:

processLoginUser($email, $password, $dataObject);

Then it will be accessible within the scope of the function when you add it to the argument list: 然后,当您将其添加到参数列表时,将可以在函数范围内对其进行访问:

function processLoginUser($email, $password, $dataObject) {
    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam()
}

Or you can use a closure ( >= PHP 5.3 ), assuming the closure is called after defining $dataObject : 或者,您可以使用闭包( > = PHP 5.3 ),假设在定义$dataObject之后调用了闭包:

$processLoginUser = function($email, $password) use ($dataObject) {
    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam()
};
$processLoginUser($email, $password); // call the closure as you would a regular function.

Or, alternatively, you can make the $dataObject variable global : 或者,您也可以使$dataObject变量为global

function processLoginUser($email, $password) {
    global $dataObject;

    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam()
}

This is purely opinion-based, but for your case, I'd stick with the first option: just pass it through to the function as an argument. 这纯粹是基于观点的,但是对于您的情况,我会坚持第一种选择:将其作为参数传递给函数。

You must write something like 你必须写类似

<?php 
require_once ("class.phpmailer.php"); 
require_once ("error_option.php"); 
require_once ("db_connection.php");

function processLoginUser($email, $password) {
    /**
    global keyword let you use variables out of the scope of 
    your block, however is no recommended
    **/
    global $dataObject;
    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam()

}

I suggest to you to rewrite your code on something like 我建议您重写类似的代码

function processLoginUser($email, $password, $dataObject) {
    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam()    
}

[1] http://php.net/manual/en/language.variables.scope.php [1] http://php.net/manual/zh/language.variables.scope.php

You are running into a variable scope issue. 您遇到了可变范围问题。 See: http://php.net/manual/en/language.variables.scope.php 参见: http : //php.net/manual/en/language.variables.scope.php

I would not recommend using globals to solve this. 我不建议使用全局变量来解决此问题。 Your best option is to pass the variable through to the function. 最好的选择是将变量传递给函数。

function processLoginUser($email, $password, $dataObject) {
    $email = trim($email);
    $password = trim($password);
    $checkUserQuery = $dataObject->prepare("CALL sp_CheckLogin()");
    $checkUserQuery->bindParam()
}

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

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