php中new stdclass()用在什麼情境

來源:互聯網
上載者:User
關鍵字 php
請問new stdclass()是什麼作用,一般用在什麼情境?
謝謝。

回複內容:

請問new stdclass()是什麼作用,一般用在什麼情境?
謝謝。

感謝提問,在stackOverFlow找到了答案,下面翻譯過來:
1、在返回特定資料類型的時候,使用stdClass,如:

$person   -> name = "John"   -> surname = "Miller"   -> address = "123 Fake St"

2、在返回同類型資料的列表時使用Array,如:

  "John Miller"  "Peter Miller"  "Josh Swanson"  "Harry Miller"

3、在返回特定類型的列表時,stdClass、array並用,如:

 $person[0]    -> name = "John"    -> surname = "Miller"    -> address = "123 Fake St"  $person[1]    -> name = "Peter"    -> surname = "Miller"    -> address = "345 High St"

個人認為:在不同的用途下使用array或stdClass,或兩者結合,更加便捷。

我常用在儲存資料 主要是開銷比較小

$obj = new stdClass;$obj->test = 'abc';$obj->other = 6.2;$obj->arr = array (1, 2, 3);

比數組更便捷

測試

$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);}

結果

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

orm中要用,把查詢的每一條紀錄轉換成一個class,每個欄位就成了屬性

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.