PHPackages                             peroks/model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. peroks/model

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

peroks/model
============

Models: Classes with built-in data validation

3.0.2(11mo ago)0732MITPHPPHP &gt;=8.0

Since Sep 28Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/peroks/model)[ Packagist](https://packagist.org/packages/peroks/model)[ RSS](/packages/peroks-model/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (13)Used By (2)

Models: Classes with built-in data validation
=============================================

[](#models-classes-with-built-in-data-validation)

The `Model` class extends the built-in PHP `ArrayObject` class with internal data validation and json support.

Reason why
----------

[](#reason-why)

Models can be used in any context, but they are especially useful in REST API applications, where validating incoming and outgoing data is often time-consuming, inconsistent, error-prone and hard to read/maintain.

Instead of bloating your project with code for validating data, you can let **models validate themselves** based on their **property definition**. You just define the properties and constraints of each model in **one single place**, and then validate model instances throughout your application in a consistent, efficient and clear manner.

Models are similar to **database tables**, where each **model property**corresponds to a **table column**. Install the [Model Store](https://packagist.org/packages/peroks/model-store)package to automatically create database tables based on your models.

How to use
----------

[](#how-to-use)

### Create a model class

[](#create-a-model-class)

You define the model properties and constraints in a **separate class** for each model. Each model class must contain the static `$properties` property and extend the `Model` class or a subclass.

In the example below, we create a geo point model with two properties: **latitude** and **longitude**. Both properties are **required**, and they must be **floating** numbers in the range of **-90 to 90** (latitude) and **-180 to 180** (longitude).

```
