2

I'm trying to pass a variable from one method to another.

I'm fetching dates from an events table in order to build a dynamic query in another method.

class Player extends Database {
    private $start_date;
    private $end_date;
    private $sumCase;

protected function getEvents() {
        $sql = "SELECT * FROM `events`";
        $stmt = $this->connect()->prepare($sql);
        $stmt->execute();
        $results = $stmt->fetchAll();

        $event_arr = array();
        foreach($results as $result) {
            $event_name = $result['event_name'];
            $this->start_date = $result['start_date'];
            $this->end_date = $result['end_date'];

            $this->sumCase[] = "SUM(CASE WHEN date BETWEEN $this->start_date AND $this->end_date THEN 1 ELSE 0 END) '$event_name'";
            $dateBetween[] = "date BETWEEN $this->start_date AND $this->end_date";
            array_push($event_arr, array(
                "name" => $result['event_name'],
                "start" => $result['start_date'],
                "end" => $result['end_date'],
            ));
        }
        return print_r($this->sumCase);
    }

this returns my sumCase array without problems, I'm now trying to access this sumCase array in another method in order to build the query:

protected function getPlayerEvents() {
        $sql = "SELECT count(chalID) as total, ";
        $sql .=' '.implode(', ', $this->sumCase); 
        $sql .="from (SELECT * FROM times where ("; //ignore this
        $sql .="date BETWEEN 159310659 AND 1593538594  OR date BETWEEN 1693538594 AND 1793538594"; //ignore this
        $sql .=") AND plaID = 1 group by chalID) A"; //ignore this

        $stmt = $this->connect()->prepare($sql);
        $stmt->execute();
        $results = $stmt->fetchAll();
        echo $this->sumCase; //returns null, how do I pass variables between methods?
    }

when I echo my sumCase array within this method, it returns null. How do I access the value of the sumCase array from the getEvents method?

5
  • Did you intend to make $sumCase into an array when you assign it a value, with the square brackets? That would affect your ability to echo it. Commented Oct 8, 2020 at 18:01
  • Please extract a minimal reproducible example and include that in your question. Also note that it's unclear why you store this in a member and return its content formatted with print_r(). As a new user here, please also take the tour and read How to Ask. Commented Oct 8, 2020 at 18:13
  • Are you using $sumCase variable in the same object context for calling in the both function ? Commented Oct 8, 2020 at 18:14
  • @MartinZeitler ya, the questioner has not clear everything in his query Commented Oct 8, 2020 at 18:25
  • There is no need to return anything, when using a property. Commented Oct 8, 2020 at 18:47

3 Answers 3

3

It returns null if function getEvents wasnt called first or it was called not to same instance of object.

<?php

use directory_with_classes\Player;

$player = new Player();
$player->getEvents();
$player->getPlayerEvents();

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

Comments

1

The way I understand your problem, you need to access a variable in a method declared in another one. In order to carry the variable over, you need to first declare the $ globally and then access it inside the functions. Example:

    $foo = NULL; // Placeholder
    function seg() {
        $foo = "something";
    }
    function bar() {
        echo $foo; // You can now access it!
    }

1 Comment

Sorry, but this is sequential code at the global scope, while the topic is OOP.
0
function __construct(){
  $this->foo = "";
}

function seg() {
  $this->foo = "something";
}

function bar() {
  echo $this->foo; // You can now access it!
}

Comments

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.