What is the function of the new Stdclass (), and what is the general use of the scene?
Thank you.
Reply content:
What is the function of the new Stdclass (), and what is the general use of the scene?
Thank you.
Thank you for asking questions, StackOverflow found the answer, the following translation here:
1. When returning a specific data type, use Stdclass, such as:
$person -> name = "John" -> surname = "Miller" -> address = "123 Fake St"
2. Use an array when returning a list of the same type of data, such as:
"John Miller" "Peter Miller" "Josh Swanson" "Harry Miller"
3. When returning a specific type of list, use StdClass, array, such as:
$person[0] -> name = "John" -> surname = "Miller" -> address = "123 Fake St" $person[1] -> name = "Peter" -> surname = "Miller" -> address = "345 High St"
Personal opinion: It is more convenient to use array or stdclass for different purposes, or combine them.
I used to save the data mainly is the cost is relatively small
$obj = new stdClass;$obj->test = 'abc';$obj->other = 6.2;$obj->arr = array (1, 2, 3);
More convenient than arrays
Test
$size = 200000;mark (), $SC = new Stdclass;foreach (range (1,5) as $i) {$SC $i = Pow ($i, 2);} Mark ();d UMP (' SC ', 1), Mark (), foreach (Range (1, $size) as $i) {$SCN = "sc". $i; $ $scn = $SC; $arr _sc[] = $SC;} Mark ();d UMP (' SC '), Mark (), $arr =[];foreach (range (1,5) as $i) {$arr [] = Pow ($i, 2);} Mark ();d UMP (' arr ', 1), Mark (), foreach (Range (1, $size) as $i) {$arrn = "arr". $i; $ $arrn = $arr; $arr _arr[] = $arr;} Mark ();d UMP (' 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);}
Results
TOTAL:0.002MB avg:1.773kb time:0
TOTAL:18.785MB avg:0.096kb time:0.101
TOTAL:0.001MB avg:1.227kb time:0
TOTAL:18.785MB avg:0.096kb time:0.1
In Orm, each record of a query is converted into a class, and each field becomes a property