Okuhle
Okuhle

Reputation: 912

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. Some of these functions will need to use the mysql database. I have created a separate db_connections.php file which I have added to the functions file, using require_once.

I can access the PDO object created in this file, but I cannot access this variable inside my functions. 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();
}

Upvotes: 2

Views: 610

Answers (3)

mferly
mferly

Reputation: 1656

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:

$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:

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.

Upvotes: 4

izzy
izzy

Reputation: 23

You are running into a variable scope issue. See: 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()
}

Upvotes: 0

Carlos
Carlos

Reputation: 4637

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

Upvotes: 1

Related Questions