codemagpie/array2object 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codemagpie/array2object

Composer 安装命令:

composer require codemagpie/array2object

包简介

A high performance, Supports converting array into object and object into array, easy to use

README 文档

README

Introduction

Supports converting array into object and object into array, easy to use

Installation

composer require codemagpie/array2object

Usage1

Only need to declare the class property type:

use CodeMagpie\ArrayToObject\AbstractBaseObject

class User extends AbstractBaseObject
{
    public string $name;
    
    public int $age;
    
    public User $child;
    /** 
     * @var User[] 
     */
    public array $children;
}

To use:

$arr = [
    'name' => 'test',
    'age' => '17',
    'child' => [
        'name' => 'test1',
        'age' => '17',
    ],
    'children' => [
        [
            'name' => 'test2',
            'age' => '17',
        ],
    ],
];
$user = new User($arr);
// if the user object does not extend AbstractBaseObject, you should use:
// $user = new User();
// \CodeMagpie\ArrayToObject\ArrayToObjectBuilder::create()->bind($user);
// echo \CodeMagpie\ArrayToObject\Utils\DataHelper::objectToArray($user); // Array
echo $user->name; // test
echo $user->age; // 17
echo $user->child->name; // test1
echo $user->children[0]->name; // test2
echo $user->toArray() //Array. if extend ArrayToObjectBuilder

Usage2

Only need declare the property type on the class's annotation:

use CodeMagpie\ArrayToObject\AbstractLightBaseObject

/**
 * @property string $name
 * @property int $age
 * @property User $child
 * @property User[] $children
 */
class User extends AbstractLightBaseObject
{
}

To use:

$arr = [
    'name' => 'test',
    'age' => '17',
    'child' => [
        'name' => 'test1',
        'age' => '17',
    ],
    'children' => [
        [
            'name' => 'test2',
            'age' => '17',
        ],
    ],
];
$user = new User($arr);
// if the user object does not extend AbstractLightBaseObject, you should use:
// $user = new User();
// \CodeMagpie\ArrayToObject\ArrayToObjectBuilder::createFormPropertyDocParser()->bind($user);
// echo \CodeMagpie\ArrayToObject\Utils\DataHelper::objectToArray($user);// Array
echo $user->name; // test
echo $user->age; // 17
echo $user->child->name; // test1
echo $user->children[0]->name; // test2
echo $user->toArray() //Array. if extend AbstractLightBaseObject

Usage performance comparison

proportion time consumption memory usage memory consumption
usage1:usage2 4:1 2:3 ≈ 1:1

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固