Get php object defined in class to javascript in separate file
<p>I haven't done much web development, to preface. I have a <code>methodExec.php</code> file and a <code>main.js</code> file. In my PHP file, I'm creating a list containing rows from a database and I'm trying to use that list in my .js file. I can access it if I declare the array outside of the class, but I'm not sure how to reference it after collecting the rows in the database.</p>

<p>methodExec.php</p>

<pre><code>&lt;?php

require_once 'includes/constants.php';

$methodExecVar = new methodExec();

class methodExec {
    private $conn;
    public $timesList;

    function __construct(){
        $this-&gt;conn = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD, DB_TIMES) or 
                die('There was a problem with the database connection.');
    }

    function getTimes(){

        $timesList = [];
        $query = "SELECT *
                    FROM times";

        $result = $this-&gt;conn-&gt;query($query);

        while ($row = $result-&gt;fetch_assoc()) {
            $timesList[] = $row;
        }

        $this-&gt;conn-&gt;close();

        return $timesList;
    }
}
?&gt;

&lt;script type="text/javascript"&gt;var jsArray = &lt;?php echo json_encode($methodExecVar-&gt;timesList); ?&gt;;&lt;/script&gt;
&lt;script type="text/javascript" src="js/main.js"&gt;&lt;/script&gt;
</code></pre>

<p>main.js only includes <code>alert(jsArray)</code>. In this instance, it returns null, which I can understand, but I'm not sure how to make it wait or any alternatives. Thanks in advance!</p>