简体   繁体   中英

Is it better to use properties only classes, if I instance them very often?

I'm pretty new to php OOP and have a general question about instancing classes.

In CodeIgniter I have created an example class with some properties and some methods. After instancing this class and setting all the properties I use the var_dump() method to see how the instanced object looks like. It dumps a really large string with many unnecessary data and sub-objects.

This is my class:

class Searchset {

    private $id;
    private $name;
    private $created;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->model('search_model');
        $this->CI->load->library('krumo');
    }

    public function getSearchSet()
    {
        return $this->CI->search_model->getSearchSet();
    }

    public function getAllSearchSetsByUserId($userId)
    {
        return $this->CI->search_model->getAllSearchSetsByUserId();
    }

    public function setId($value)
    {
        $this->id = $value;
    }

    public function setName($value)
    {
        $this->name = $value;
    }

    public function setCreated($value)
    {
        $this->created = $value;
    }
}

I changed this above class to the following class:

class Test {

    private $id;
    private $name;
    private $created;

    public function setId($value)
    {
        $this->id = $value;
    }

    public function setName($value)
    {
        $this->name = $value;
    }

    public function setCreated($value)
    {
        $this->created = $value;
    }
}

When I create now an instance and set all the properties, I get a much smaller var_dump with the properties only (that's what I want to achieve):

object(Test)#24 (3) { ["id":"Test":private]=> string(1) "1" ["name":"Test":private]=> string(16) "Test Searrch Set" ["created":"Test":private]=> string(19) "2019-06-06 11:45:25" }

I instance this class very often (I build large arrays of these objects) and don't want them to be too large (performance). Is there a way to create a 'small' instance including only the properties with the first shown class (searchSet)? Or should I create 2 classes? One for the methods and the second one only for properties?

How should I handle this problem?

It is not so much a undesired result as it is a result of loading the CI instance which contains all of its objects. If you need to access CI objects then you have to use it, if you don't... well you don't.

You can make your code slightly more efficient by not assigning the CI instance in the constructor, but instead in the functions that you need to access some CI object in.

class Test {

    public function cinotneeded() {
        echo 'hello world';
    }

    public function cineeded() {
        $CI = &get_instance();
        return $CI->db->get('blog')->result();
    }

}

But to be honest, I doubt you'll see much of a performance gain by doing it this way vs the constructor way. As you said, and as your code suggests, some functions set properties, and some execute blocks of code. As such, you won't see any performance boost because they would typically be used in such a fashion (in the same instance): $this->set('foo', 'bar')->get('blog');

Where set would set foo to bar and those properties would be used to get a result from blog . So CI is always involved here at some level.

You might see some (negligible) amount of performance gain, if you had a function that simply returned something unrelated to CI.

eg

function controller() {
    $this->load->library('somelibrary');
    echo $this->somelibrary->get_rand();
}

// library function
function get_rand() {
    return rand();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM