Class mechanism problems

Source: Internet
Author: User
Category and mechanism problems the previous post is no longer popular. the question to be asked is not solved (it is estimated that the question is not clearly expressed). let's start with two categories, these two classes mainly use different sources of source data. Test1 class PHPcodeclasstest1 {protected $ arrarray (); function _ construct ($ arr) {$ this-& gt; arr $ arr; class mechanism problems
The previous post is no longer popular. the problem to be asked is not solved (it is estimated that it is not clear)

First, let's take two classes. These two classes mainly refer to different sources of source data.

Test1 class
PHP code
  class test1{     protected $arr = array();     function __construct($arr){        $this->arr = $arr;    }     function t1(){        //use $this->arr    }     function t2(){        //use $this->arr    } }


Test2 type
PHP code
  class test2{     function get_arr(){        //get $arr from data        return $arr;    }     function t1(){        //use $this->get_arr())    }     function t2(){        //use $this->get_arr()    } }


The difference between the two classes is the difference in the way the source data is obtained.
Test1 directly exists in the class property and is obtained through initialization.
Test2 is obtained from the file through the get_arr function

The problem is,
1. if all the functions in a class (such as t1) are called, the test1 class except the source data $ arr is earlier than test2 (the source data $ arr will be available at test1 initialization, what is the difference (only efficiency and memory) between test2 and get_arr )?

II. this is mainly because if both t1 and t2 are called on a page, there should be only one copy of arr data for the test1 class, and test2 calls get_arr twice, are there two $ arr arrays in the memory ??? If so, is it better to put it in class attributes?

Address of the previous post



------ Solution --------------------
You can test both questions.

If you understand copy on write, you will know that simply calling an array will not cause data copy in the memory.

But when you change the operation values in class methods and functions, you do not need to save the original data. Naturally, test1 is better.
Similar to test1,
Function t (& $ arr) is used to avoid writing large data copies .... Php design is still quite clever
------ Solution --------------------
In other words, the second type usually needs to be cached, so it is impossible to directly read the file every call.
So there is no difference.
PHP code
Class test2 {public static $ data = array (); function get_arr () {if (self: $ data) return self: $ data; // get $ arr from data return $ arr;} function t1 () {// use $ this-> get_arr ()} function t2 () {// use $ this-> get_arr ()}}
------ Solution --------------------
I think both of your solutions are not good, but they are better integrated.
PHP code
class test1{    protected $arr = array();    function __construct(){        $this->arr = get_arr();    }    function get_arr(){        //get $arr from data        return $arr;    }    function t1(){        //use $this->arr    }    function t2(){        //use $this->arr    }} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.