Note that there are some explanatory texts on larger screens.

plurals
  1. POOOP PHP - Get vars from one class and into a class within another file
    primarykey
    data
    text
    <p>I have a situation where I'm creating a controller file that echo's json output to be used client-side with ajax: <code>echo json_encode($response);</code></p> <p>The main class in another file, among other things, grabs all the setting vars from the CMS.</p> <p>Now, the controller file has a class that generates the request from the API, but the setting vars in the class <code>(username, id, count, etc.)</code> are hard coded because I can't figure out how exactly to get them from the main class in the other file. With the settings hard coded, the controller file creates and echos the json output as expected. It just needs the dynamic vars from the main class.</p> <p>Please excuse my lack of knowledge and usage of OOP. I've been trying it with a structure like this, where again, just trying to get the username and other vars from the main class into another class within separate file.</p> <p>** <strong>EDIT</strong> ** Rethinking this a bit based on the comment by <a href="https://stackoverflow.com/users/1208233/dave-just">@Dave Just</a> as it makes better sense. So if I move the api_request function into mainClass and <code>return</code> the response, I can get the variables I need and the request still works. So that would lead me to ask - how can I still echo the <code>$response</code> from the api_request function in a separate file? That separate file with the json is what I'm using for my ajax script.</p> <pre><code>class mainClass { public $username; function __construct() { ... } public function api_settings( $username ) { ... } } $main_class = new mainClass; $main_class-&gt;api_settings(); // OR $main_class-&gt;username; </code></pre> <p><strong>api-call.php</strong></p> <pre><code>class apiCall extends mainClass { public $username; function __construct() { parent::__construct; ... } public function api_request() { ... $return = $api_auth-&gt;request( 'GET', $api_auth-&gt;url( '/cms-plug' ), array( //where I need to be able to grab the $username from the main class 'username' =&gt; 'joebob' ) ); echo json_encode($response); } } $api_class = new apiCall; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload