What is the role of newstdclass () And what scenarios are generally used? Thank you. What is the role of new stdclass ()? What are the common scenarios?
Thank you.
Reply content:
What is the role of new stdclass ()? What are the common scenarios?
Thank you.
Thanks for your question. The answer is found in stackOverFlow:
1. Use stdClass when returning a specific data type, for example:
$person -> name = "John" -> surname = "Miller" -> address = "123 Fake St"
2. Use Array when returning a list of the same type of data, for example:
"John Miller" "Peter Miller" "Josh Swanson" "Harry Miller"
3. stdClass and array are used when a list of specific types is returned, for example:
$person[0] -> name = "John" -> surname = "Miller" -> address = "123 Fake St" $person[1] -> name = "Peter" -> surname = "Miller" -> address = "345 High St"
I personally think that it is more convenient to use array or stdClass for different purposes, or to combine the two.
I usually save data with relatively low overhead.
$obj = new stdClass;$obj->test = 'abc';$obj->other = 6.2;$obj->arr = array (1, 2, 3);
Easier than Array
Test
$size = 200000;mark();$sc = new stdClass;foreach(range(1,5) as $i){ $sc->$i = pow($i,2);}mark();dump('sc',1);mark();foreach(range(1,$size) as $i){ $scn = "sc" . $i; // $$scn = $sc; $arr_sc[] = $sc;}mark();dump('sc');mark();$arr =[];foreach(range(1,5 ) as $i){ $arr[] = pow($i,2);}mark();dump('arr',1);mark();foreach(range(1,$size) as $i){ $arrn = "arr" . $i; // $$arrn = $arr; $arr_arr[] = $arr;}mark();dump('arr');function dump($var,$size = 200000){ global $mem_size,$times,$$var; // xdebug_debug_zval($var); echo "Total:" .round(($mem_size[count($mem_size)-1]-$mem_size[count($mem_size)-2])/pow(1024,2),3) . "Mb " . "Avg:". round(($mem_size[count($mem_size)-1]-$mem_size[count($mem_size)-2])/(1024*$size),3) . "Kb " . "Time:". round(($times[count($times)-1] -$times[count($times)-2]),3) ."\n";}function mark($type=0){ global $mem_size,$times; $mem_size[] = memory_get_usage(); $times[] = microtime(true);}
Result
Total: 0.002 Mb Avg: 1.773Kb Time: 0
Total: 18.785 Mbit/s Avg: 0.096Kb Time: 0.101
Total: 0.001 Mb Avg: 1.227Kb Time: 0
Total: 18.785 Mbit/s Avg: 0.096Kb Time: 0.1
To be used in the orm, convert each record to a class, and each field becomes an attribute.