I have made a library class in php which can get data from my database table settings.

I can get information if it is not serialized.

But I would like to know on my get function what would be best method to be able to return unserialise information just incase it is serialized.

<?php

class Settings {

private $data = array();

public function __construct() {
$this->CI =& get_instance();      
}

public function get($key = NULL) {
if ($key != NULL) {
$value = $this->get_where($key);
return $value;
} else {
return FALSE;
}
}

public function get_where($key = NULL) {
if ($key != NULL) {
$store_id = $this->store_id();
$result = $this->CI->db->get_where($this->CI->db->dbprefix . 'setting', array('store_id' => $store_id, 'group' => 'config', 'key' => $key));
if ($result != FALSE AND $result->num_rows() > 0) { 
$data = $result->result_array();
return $data[0]['value'];
} else {
return FALSE;
}
} else {
return FALSE;
} 
}

public function store_id() {
$store_id = $this->CI->uri->segment(5);
if ($store_id !== TRUE) {
return $store_id;
} else {
return FALSE;
}
}

}

I would probably just unserialize the value, regardless of whether it is serialized or not, since I expect it to always be unserialized.

public function get($key = NULL) {
    if ($key != NULL) {
        $value = $this->get_where(@unserialize($key));
        return $value;
    } else {
        return FALSE;
    }
}

Also, I've used this function in the past to check if a string is serialized.

function isSerialized($str) {
    return ($str == serialize(false) || @unserialize($str) !== false);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.