Andy_Kemp Posted July 21, 2016 Share Posted July 21, 2016 In Wordpress there is this code <?php $user_info = get_userdata(1); echo 'Username: ' . $user_info->user_login . "\n"; echo 'User roles: ' . implode(', ', $user_info->roles) . "\n"; echo 'User ID: ' . $user_info->ID . "\n"; ?> My question is how they get this? $user_info->user_login My code class Config { private $db; public function __construct($db) { $this->db = $db; } public function get() { $query = 'SELECT * FROM config'; $select = $this->db->query($query); $config = array(); while ($row = $select->fetch(PDO::FETCH_OBJ)) { $config[$row->config] = $row->value; } return $config; } } $config = new Config($db); $conf = $config->get(); print $conf['test']; Now i liked to do same way like wordpress Instead of this $conf['test']; i'd like to get result like this $conf->test; Quote Link to comment https://forums.phpfreaks.com/topic/301536-need-help-with-php/ Share on other sites More sharing options...
cyberRobot Posted July 21, 2016 Share Posted July 21, 2016 You would use a class property. More information can be found here: http://php.net/manual/en/language.oop5.properties.php Quote Link to comment https://forums.phpfreaks.com/topic/301536-need-help-with-php/#findComment-1534795 Share on other sites More sharing options...
ginerjm Posted July 21, 2016 Share Posted July 21, 2016 You're talking about a minor difference in notation/handling here. Is it necessary? Really? Or you just like the look of it. If you need to do it, add a public property to your class. Then instead of assigning the query result to an array assign it to the caller's property. Voila. Of course if you don't understand this, maybe you don't want to get into OOP Quote Link to comment https://forums.phpfreaks.com/topic/301536-need-help-with-php/#findComment-1534797 Share on other sites More sharing options...
Destramic Posted July 21, 2016 Share Posted July 21, 2016 your not going to achieve $conf->test when selecting multiple rows. i'd stick with $config['test'] mate Quote Link to comment https://forums.phpfreaks.com/topic/301536-need-help-with-php/#findComment-1534799 Share on other sites More sharing options...
ginerjm Posted July 21, 2016 Share Posted July 21, 2016 Hah!! Never even looked at his query. Good point! Quote Link to comment https://forums.phpfreaks.com/topic/301536-need-help-with-php/#findComment-1534800 Share on other sites More sharing options...
Psycho Posted July 21, 2016 Share Posted July 21, 2016 (edited) your not going to achieve $conf->test when selecting multiple rows. i'd stick with $config['test'] mate Of course you can. Create an object with a different property for each 'config' name. (EDIT: Note that it does not have to be the same object used to get the data. Instead create a 'standard' dynamic class in which to populate the data and return to the caller) Not tested, but the logic should be sound class Config { private $dbObj; public function __construct($db) { //Set db object $this->dbObj = $db; //Call get method and return result return $this->get(); } public function get() { //Run query to get data $query = 'SELECT config, value FROM config'; $select = $this->dbObj->query($query); //Create object and populate with query results $configObj = new stdClass(); while ($row = $select->fetch(PDO::FETCH_OBJ)) { $configObj->$row->config = $row->value; } //Return object return $configObj; } } $config = new Config($db); echo $config->test; //Where 'test' is the name of any 'config' field from the DB results Edited July 21, 2016 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/301536-need-help-with-php/#findComment-1534801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.