简体   繁体   English

如果我经常实例化它们,最好只使用属性类吗?

[英]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.我对 php OOP 很陌生,并且有一个关于实例化类的一般问题。

In CodeIgniter I have created an example class with some properties and some methods.在 CodeIgniter 中,我创建了一个带有一些属性和一些方法的示例类。 After instancing this class and setting all the properties I use the var_dump() method to see how the instanced object looks like.在实例化此类并设置所有属性后,我使用 var_dump() 方法查看实例化对象的外观。 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):当我现在创建一个实例并设置所有属性时,我得到了一个更小的 var_dump,只有这些属性(这就是我想要实现的):

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" } object(Test)#24 (3) { ["id":"Test":private]=> string(1) "1" ["name":"Test":private]=> string(16) "Test Search 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)?有没有办法创建一个“小”实例,只包含第一个显示的类(searchSet)的属性? Or should I create 2 classes?或者我应该创建 2 个类? 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.这与其说是不受欢迎的结果,不如说是加载包含其所有对象的 CI 实例的结果。 If you need to access CI objects then you have to use it, if you don't... well you don't.如果您需要访问 CI 对象,那么您必须使用它,如果您不需要……那么您不需要。

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.您可以通过不在构造函数中分配 CI 实例,而是在需要访问某些 CI 对象的函数中分配 CI 实例,从而稍微提高代码效率。

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');因此,您不会看到任何性能提升,因为它们通常会以这种方式使用(在同一实例中): $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 .其中 set 会将foo设置为bar ,这些属性将用于从blog获取结果。 So CI is always involved here at some level.因此,CI 总是在某种程度上参与其中。

You might see some (negligible) amount of performance gain, if you had a function that simply returned something unrelated to CI.如果您的函数只返回与 CI 无关的内容,您可能会看到一些(可以忽略不计)的性能提升。

eg例如

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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