定制 actualwave/object 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

actualwave/object

Composer 安装命令:

composer require actualwave/object

包简介

Non dynamic base Object class with getters/setters implementation

README 文档

README

Build Status Coverage Status Dependency Status Latest Stable Version Total Downloads License

Non dynamic base object class for PHP. Allows making getters and setters via get* and set* methods with public/protected accessor. Not defined properties will throw error.

Installation

Via composer

composer require actualwave/object

Usage

Basically instead of shared magic methods __set, __get, __isset, __unset you can define individual methods for each property:

  • get* - get[Property name from upper-case char], for reading property value.
  • set* - set[Property name from upper-case char], for setting property new value.
  • has* - has[Property name from upper-case char], for checking is property set.
  • remove* - remove[Property name from upper-case char], for removing(using unset() on it) property.

has* and remove* methods have default action and are optional. By default, has* will always return true for properties with defined getter and remove* will try to pass null into setter.

class MyObject extends \aw\Object {
    private $_property = null;
    public function getProperty(){
        return 'GET-'.$this->_property;
    }
    public function setProperty($value){
        $this->_property = 'SET-'.$value;
    }
    public function getData(){
        return 'DATA:'.$this->hiddenProperty;
    }
    protected function getHiddenProperty(){
        return 'hidden value';
    }
}

Instances of MyObject will be non-dynamic objects with one read-write property property that can be accessed via $instance->property and two read-only properties -- hiddenProperty and its alias data.

$instance = new MyObject();
echo $instance->property.PHP_EOL; // GET-
$instance->property = 'something';
echo $instance->property.PHP_EOL; // GET-SET-something
echo $instance->getData().PHP_EOL; // DATA:hidden value
echo $instance->data.PHP_EOL; // DATA:hidden value
echo $instance->anyProperty.PHP_EOL; // throws error Property accessor "anyProperty" not found.

You can change behaviour of your property when isset/unset are used via has* and remove* methods.

class StringProperty extends \aw\Object {
    private $_property = '';
    public function getProperty():string {
        return $this->_property;
    }
    public function setProperty(string $value) {
        $this->_property = $value;
    }
    protected function hasProperty():bool {
        return (bool)$this->_property;
    }
    protected function removeProperty() {
        $this->_property = '';
    }
}

Then check if your property is empty or set it to be empty:

$prop = new StringProperty();
echo json_encode(isset($prop->property)).PHP_EOL; // false
$prop->property = 'value';
echo json_encode(isset($prop->property)).PHP_EOL; // true
unset($prop->property);
echo json_encode($prop->property).PHP_EOL; // "" -- will output empty string in JSON format
echo json_encode(isset($prop->property)).PHP_EOL; // false

Note: Defining has* and remove* methods is optional, but without them you will not be able to define logic for isset() and unset() actions over your property. Without them unset/delete property via unset() just tries to pass null into property mutator method and even if property set to null, isset() will always return true:

class MySimpleObject extends \aw\Object {
    private $_property = null;
    public function getProperty(){
        return $this->_property;
    }
    public function setProperty($value){
        $this->_property = $value;
    }
}

$simple = new MySimpleObject();
$simple->property = 'something';
echo 'Is set: '.json_encode(isset($simple->property)).PHP_EOL; // Is set: true
echo 'Is null: '.json_encode(is_null($simple->property)).PHP_EOL; // Is null: false
unset($simple->property);
echo 'Is set: '.json_encode(isset($simple->property)).PHP_EOL; // Is set: true
echo 'Is null: '.json_encode(is_null($simple->property)).PHP_EOL; // Is null: true

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固