title: Cakephp: variable data in appController is not consistent in Xcontroller child
body: <p>I am using the Cakephp (2.X) framework.</p>
  
  <p>I have created a facebook canvas app.  I have my facebook object as a variable in appController.  I instantiate it in the constructor. In my usersController I am trying to use this object.  The object is there, though not all of the data within the facebook object is persistent.   </p>
  
  <p>For instance if I var dump facebook object in appController all of the properties are present.  When I dump the facebook object in my other controller some of the properties are null/0/not present.  For instance 'user', 'signedRequest' and 'accessToken'.</p>
  
  <p>Currently this is what my files look like:</p>
  
  <hr>
  
  <p>appController.php</p>
  
  <pre><code>class AppController extends AppController {
      public $facebook;
      //other vars here
  
      function __construct($request = null, $response = null) {
          parent::__construct($request, $response);
  
          App::import('Vendor','fb/src/facebook');
          ...
          $this-&gt;facebook = new Facebook(//array with appId and Secret);
          // doing stuff here with $this-&gt;facebook
      }           
  
     //other methods
  </code></pre>
  
  <p>And the facebook object is fully defined in every method in this class</p>
  
  <hr>
  
  <p>UsersController.php</p>
  
  <pre><code>class UsersController extends Controller {
      //some functions
  
      function useParentsFBvar(){
          //I have tried
          $this-&gt;facebook;
  
          //and I have tried
          parent::facebook;
  
      }
  </code></pre>
  
  <p>Both of the above are not fully defined facebook objects.  Specifically 'user', 'signedRequest', and 'accessToken'.</p>
  
  <p>I have even tried implementing a function in appController and calling from my UsersController</p>
  
  <pre><code>function getFB() {
      return $this-&gt;facebook;
  }
  </code></pre>
  
  <p>But I get the same issue.</p>
  
  <p>Is there something obvious that I am doing wrong here?</p>
  
  <p>Oh and this is my first question and I am very new to cakephp and PHP so please be easy on me.</p>
  
  <p>If you need any other info please let me know.  I searched for similar questions but did not find any suitable...sorry if this is a repeat.</p>
  
  <h2>------------------------EDIT--------------------------</h2>
  
  <h2>This is what I've done to get it to work as I wanted.  And the reasons I did things Can someone confirm whether this breaks any patterns or if this is acceptable MVC</h2>
  
  <p>What I did was convert facebook object in appController to a static member.  I then new it in the constructor after checking to see if it has already been new'd.  If it has already been new'd I do nothing.  once I made this change it made the facebook object in UsersController.  </p>
  
  <pre><code>    &lt;               appController.php      &gt;
  
  App::uses('Controller', 'Controller');
  App::import('Vendor','fb/src/facebook');    
  
  class AppController extends Controller {
      public static $facebook = null;
      //other members, components and helpers
  
  
      function __construct($request = null, $response = null){
          parent::__construct($request,$response);
  
          //load the configurations for app
  
          if(!static::$facebook){
              static::$facebook = new Facebook(
                                  array('appId' =&gt; $this-&gt;app_id,
                                          'secret' =&gt; $this-&gt;secret,
                                          'cookie'=&gt; true));
  
          }
        }
  
        function beforeFilter() {
            //get the user and do some things with static::facebook
        }
  </code></pre>
  
  <p>Then in UserController.php $this->facebook gave me a valid object.  Once I got that working I figured that it made sense to move the logic into the user model so I created a model function to return X number of friends using the app.  </p>
  
  <pre><code>    &lt;---------------UsersController.php------------------&gt;
  class UsersController extends AppController {
      public $name = 'Users';
  
      public function friends($count = 5) {
          $this-&gt;set('friends', $this-&gt;User-&gt;getFriendsUsingApp($this-&gt;facebook, $count));
      }
  }
  </code></pre>
  
  <p>and in </p>
  
  <pre><code>    &lt;-------------------User.php---------------------------&gt;
  class User extends AppModel {
  
      public function getFriendsUsingApp($facebook, $max = 5) {
          $installedFriends = array();
  
          try {
              $installedFriends = $facebook-&gt;api('/me/friends?fields=installed');
          } catch (FacebookApiException $e) {
              $this-&gt;log($e-&gt;getMessage());
          }
  
          //search through the array of all returned friends and search for "ionstalled" field
          $friends = array();
  
          foreach ($installedFriends['data'] as $friend) {
              if (array_key_exists('installed',$friend) &amp;&amp; $friend['installed']) {
                  $friends[] = $friend;
              }
          }
          return $friends;
      }
  }
  </code></pre>
  
  <p>This has been working fine and I feel abides by MVC by moving some more of the logic into the model (though uncertain) please tell me if I am correct.</p>
  
  <p>I think the problem was that when appController is first constructed it creates a new facebook object (with all valid data [accessToken and user]).  Then when my userController instance was being constructed a new appController was constructed thus creating another new facebook object.  Somehow when the second call to new Facebook(...) was called it was returning some type of only partially valid object (appId and secret were now each arrays, and there was no accessToken or User. So I made facebook static and now all controllers share the same copy of the facebook object.</p>
  
  <pre><code>&lt;-----facebook object dumped in AppController---------&gt;
  (
      [appId:protected] =&gt; //appId//
      [appSecret:protected] =&gt; //secret//
      [user:protected] =&gt; //an actual userID//
      [signedRequest:protected] =&gt; 
      [state:protected] =&gt; //state//
      [accessToken:protected] =&gt; //an actual accessToken//
      [fileUploadSupport:protected] =&gt; 
  )   
  
  &lt;-----facebook object on dump in UsersController---------&gt;
  (
      [appId:protected] =&gt; array(
                                  [0] =&gt; //appId//
                                  [1] =&gt; //appId//
                                 )
      [appSecret:protected] =&gt; array(
                                  [0] =&gt; //secret//
                                  [1] =&gt; //secret//
                                 )
      [user:protected] =&gt; array|array
      [signedRequest:protected] =&gt; 
      [state:protected] =&gt; //state//
      [accessToken:protected] =&gt; array|array -//though sometimes just blank
      [fileUploadSupport:protected] =&gt; 
  )
  </code></pre>
  
  <p>Hope this makes sense.</p>