Jump to content

Need help with php

Recommended Posts

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?


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


i'd like to get result like this

Link to post
Share on other sites

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

Link to post
Share on other sites

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 by Psycho
  • Like 1
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.