php中new stdclass()用在什么场景
请问new stdclass()是什么作用,一般用在什么场景?
谢谢。
回复内容:
请问new stdclass()是什么作用,一般用在什么场景?
谢谢。
感谢提问,在stackOverFlow找到了答案,下面翻译过来:
1、在返回特定数据类型的时候,使用stdClass,如:
<code>$person -> name = "John" -> surname = "Miller" -> address = "123 Fake St"</code>
2、在返回同类型数据的列表时使用Array,如:
<code> "John Miller" "Peter Miller" "Josh Swanson" "Harry Miller"</code>
3、在返回特定类型的列表时,stdClass、array并用,如:
<code> $person[0] -> name = "John" -> surname = "Miller" -> address = "123 Fake St" $person[1] -> name = "Peter" -> surname = "Miller" -> address = "345 High St"</code>
个人认为:在不同的用途下使用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,每个字段就成了属性

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
