What scenario is used for new Stdclass () in PHP

Source: Internet
Author: User
Keywords Php
Tags pow
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

  • Related Article

    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.