1

Working on my first OOP app and I am having some trouble accessing the values of an array returned by a public function within my class. Here is the function -

//Process new transaction
    public function addTransaction($amount, $payee, $category, $date, $notes) {
        global $db;
        //Check to see if payee exists in Payee table
        $checkPayee = $db->prepare("SELECT * FROM payees WHERE name = ?");
        $checkPayee->execute(array($payee));
        $checkPayeeNum = $checkPayee->fetchColumn();
        $payeeDetails = $checkPayee->fetch();
        if ($checkPayeeNum < 1) {
            $insertPayee = $db->prepare("INSERT INTO payees (name, cat) VALUES(?, ?)");
            $insertPayee->execute(array($payee, $cat));
        } else {
            if ($payeeDetails['cat'] == "") {
                $updatePayee = $db->prepare("UPDATE payees SET cat=? WHERE name=?");
                $updatePayee->execute(array($cat, $payee));
            }
        }
        //Process the transaction
        $proc = $db->prepare("INSERT INTO transactions (amount, payee, cat, date, notes) VALUES (?, ?, ?, ?, ?)");
        $proc->execute(array($amount, $payee, $cat, $date, $notes));
        //Prepare array for JSON output
        $todaysTrans = $this->fetchDailyTotal();
        $weeklyTotal = $this->fetchWeeklyTotal();
        $accountBalance = $this->balanceAccount();
        $jsonOutput = array("dailyTotal" => $todaysTrans, "weeklyTotal" => $weeklyTotal, "accountBalance" => $accountBalance);
        return $jsonOutput; 
    }

Instantiating the object is not the issue, trying to figure out how to access the $jsonOutput array. How would one accomplish this?

Thanks!

4
  • You can get the result by calling this.addTransaction($amount, $payee, $category, $date, $notes) within your class. Commented Sep 22, 2011 at 1:14
  • To clarify, I need to access the array in another PHP file in which the class is included. So I am instantiating the object, calling the function, and I am stuck trying to access the returned array and its values Commented Sep 22, 2011 at 1:16
  • How are you trying to access the array in the file in which you have included the class. code? Commented Sep 22, 2011 at 1:22
  • The function in the class returns an array, $jsonOutput. The only way I can think of to access the values of this array is as follows (throws an error) - $addTrans = new addTransaction($amount, $payee, $category, $date, $notes); Then, to access the values of the array, I am trying this $payee = $addTrans['payee'] Commented Sep 22, 2011 at 1:32

1 Answer 1

2
// In some other PHP file...
include 'YourClass.php';

$yourObject = new YourClass();
$returnedArray = $yourObject->addTransaction(...);

// Access the returned array values
echo 'Daily Total: ', $returnedArray['dailyTotal'], "\n";
echo 'Weekly Total: ', $returnedArray['weeklyTotal'], "\n";
echo 'Account Balance: ', $returnedArray['accountBalance'], "\n";

Also, for what it's worth, it's very confusing for you to be returning a PHP array called $jsonOutput, as it's not JSON encoded, which is what most developers will expect it to be. If you're wanting it to be JSON encoded, use json_encode() (see here for more info).

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh of course. Such a simple thing to miss. And good call on the name of the array - in the file that the class is included into, I am encoding JSON. I used that variable name in the class to remind myself of that but I may reconsider. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.