Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure I understand the question, however, if you are using php 5, you can use the __set magic method to help with this.</p> <p>Modifying your current class:</p> <pre><code>class User { private $id; private $data = array(); public function __construct() { global $session; $this-&gt;id = $session-&gt;get('auto_id'); $this-&gt;data = array( 'disp_name'=&gt;$session-&gt;get('disp_name'), 'pic_url'=&gt;$session-&gt;get('pic_url'), 'gender'=&gt;$session-&gt;get('gender'), 'user_role'=&gt;$session-&gt;get('user_role'), 'lat'=&gt;$session-&gt;get('lat'), 'long'=&gt;$session-&gt;get('long'), 'newregister'=&gt;$session-&gt;get('newregister') ); } // return the user id public function id() { return $this-&gt;id; } // the __get magic method is called when trying to retrieve a value of a // property that has not been defined. public function __get($name) { if(array_key_exists($name, $this-&gt;data)) { return $this-&gt;data[$name]; } return null; } // the __set magic method is called when trying to store a value in a property // that has not been defined. public function __set($name, $value) { global $session; // check if the key exists in the 'data' array. // if so, set the value in the array as well as the session if(array_key_exists($name, $this-&gt;data)) { $this-&gt;data[$name] = $value; $session-&gt;set($name, $value); } } } </code></pre> <p>This way you can still get and set values the same as you were, but will also store the set the value in your session class.</p> <p>To test this:</p> <pre><code>$user = new User; if($user-&gt;id()) { echo $user-&gt;disp_name; $user-&gt;disp_name = 'new name'; echo $session-&gt;get('disp_name'); } </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.
    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