rumd3x / php-baseobject
一个非常有用的类,可以从中扩展并用作其他类的基类。
1.0.0
2018-12-01 20:35 UTC
Requires
- php: >=5.5
This package is auto-updated.
Last update: 2024-09-29 05:02:06 UTC
README
一个非常有用的类,可以从中扩展并用作其他类的基类。
安装
要使用composer安装,只需运行
composer require rumd3x/php-baseobject
使用方法
继承内容
只需创建一个继承自BaseObject的类
use Rumd3x\BaseObject\BaseObject; class MyClass extends BaseObject { // Your class definition private $my_property; }
自动获取器和设置器
要使用自动获取器和设置器,请将属性设置为snake case,方法设置为camel case。
$obj = new MyClass; $obj->my_property = 'value'; // This will automatically do $obj->setMyProperty('value'); if you have defined this method. $prop_value = $obj->my_property; // This will automatically do $prop_value = $obj->getMyProperty(); if you have defined this method.
辅助工具
在基对象上定义了几个标准辅助方法,用于对象操作,以及已定义的php魔法方法。
$obj = new MyClass; // BaseObject::parse($data) Creates a new instance base on the contents of $data. // Data can be either an object of any class, an array, a json string or a XML string. MyClass::parse($data); // Returns instance of MyClass. $obj->parse($data); // Casts $data into the already existing instance. $obj->toArray(); // Converts the object to an array. $obj->toJson(); // Converts the object to a json string representation. $obj->toXml(); // Converts the object to a XML string representation. // You can also safely use the object to along side with strings. echo $obj; $string = "my object value => {$obj}";