承接 phputil/traits 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phputil/traits

最新稳定版本:1.3

Composer 安装命令:

composer require phputil/traits

包简介

Useful traits for PHP

README 文档

README

Useful traits for PHP.

Build Status

We use semantic version. See our releases.

Installation

composer require phputil/traits

Traits

Examples

Example on GetterBuilder:

use phputil\traits\GetterBuilder;

class MyClass {

	use GetterBuilder; // simulate getters
	
	private $name = '';
	private $description = '';
	
	function __construct( $name, $description ) {
		$this->name = $name;
		$this->description = $description;
	}
}
 
$obj = new MyClass( 'Bob', 'I am Bob' );
echo $obj->getName(); // Bob
echo $obj->getDescription(); // I am Bob

Example on WithBuilder:

use phputil\traits\WithBuilder;

class MyClass {

	use WithBuilder;
	
	public $name = '';
	public $description = '';
}

$obj = ( new MyClass() )->withName( 'Bob' )->withDescription( 'I am Bob' );
echo $obj->name; // Bob
echo $obj->description; // I am Bob

Example on GetterSetterWithBuilder:

use phputil\traits\GetterSetterWithBuilder;

class MyClass {

	use GetterSetterWithBuilder;
	
	private $name = '';
	private $description = '';
}
  
$obj = ( new MyClass() )->withName( 'Bob' )->setDescription( 'I am Bob' );
echo $obj->getName(); // Bob
echo $obj->getDescription(); // I am Bob
$obj->setName( 'Bob Dylan' );
echo $obj->getName(); // Bob Dylan

Example on FromArray:

use phputil\traits\FromArray;

class MyClass {

	use FromArray;
	
	private $id;
	protected $name;
	public $age;
}
  
$obj = new MyClass();
$obj->fromArray( array( 'id' => 10, 'name' => 'Bob', 'age' => 18 ) );
var_dump( $obj ); // the attributes will have the array values

Example on converting from a dynamic object:

// From a converting from a dynamic object, just use a type casting
$p = new \stdClass;
$p->id = 10;
$p->name = 'Bob';
$p->age = 18;

$obj = new MyClass();
$obj->fromArray( (array) $p ); // Just make a type casting to array ;)

Example on ToArray:

use phputil\traits\ToArray;

class MyClass {

	use ToArray;
	
	private $id = 50;
	protected $name = 'Bob';
	public $age = 21;
}
  
$obj = new MyClass();
var_dump( $obj->toArray() ); // array( 'id' => 50, 'name' => 'Bob', 'age' => 21 )

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3
  • 更新时间: 2016-08-15