Code Monkey home page Code Monkey logo

properties's Introduction

PHP Properties

Travis CI Scrutinizer CI Latest Stable Version Latest Unstable Version License MIT Total Downloads

PHP Properties implementations based on getters or setters method and used PSR-5 PHPDoc information.

Installation

composer require serafim/properties

Package on packagist.org

Introduction

Properties provide the ability to control the behavior of setting and retrieving data from an object fields.

There are no properties at the language level, but they can be implemented with the some additional functionality.

For example:

class MyClass
{
    protected $a = 23;

    public function __get($field)
    {
        return $this->$field;
    }
}

$dto = new MyClass();
echo $dto->a; // 23
$dto->a = 42; // Cannot access protected property MyClass::$a

This code example does not provide type-hint and autocomplete. Using magic without the ability to control it is always a bad option. ...well, it looks awful %)

We can fix some problems using PSR-5. In this case, we can add a docblock.

/**
 * @property-read int $a
 */
class MyClass
{
    // ...

But this docblock only adds information for the IDE and does not affect the code itself. At the same time, you should always keep it up to date.

But wait! We have another problem.

$dto = new MyClass();
echo isset($dto->a); // false

This is obviously a bug. We still need to add __isset, __unset and __set support.

It's damn awful!!111

Properties Usage

Now let's see what the serafim/properties package provides.

/**
 * @property $a
 */
class MyClass
{
    use Serafim\Properties\Properties;
    
    protected $a = 23;
}

$dto = new MyClass();
$dto->a;      // 23
$dto->a = 42; // Ok
$dto->a;      // 42

Readonly Properties

To indicate that the type should be readonly use @property-read annotation.

/**
 * @property-read $a
 */
class MyClass
{
    use Serafim\Properties\Properties;
    
    protected $a = 23;
}

$dto = new MyClass();
$dto->a;      // 23
$dto->a = 42; // Error: Property MyClass::$a is readonly

Writeonly Properties

To indicate that the type should be readonly use @property-write annotation.

/**
 * @property-write $a
 */
class MyClass
{
    use Serafim\Properties\Properties;
    
    protected $a = 23;
}

$dto = new MyClass();
$dto->a = 42; // 42
$dto->a;      // Error: Property MyClass::$a is writeonly

Getters And Setters

For getter or setter override just declare get[Property] (is[Property] for bool values will be works too) or set[Property] methods.

/**
 * @property-read int $a
 */
class MyClass
{
    use Serafim\Properties\Properties;
    
    protected $a = 23;

    protected function getA() 
    {
        return $this->a + 19;
    }
}

$dto = new MyClass();
echo $dto->a; // 42 (because 23 + 19 = 42)
$dto->a = 42; // Error: Property is read-only (@property-read doc declaration)

Setter:

/**
 * @property-write string $anotherProperty
 */
class Some 
{
   // ... 
   protected $anotherProperty = 'some';
   
    /**
     * @param string $newVal
     */
   public function setAnotherProperty($newVal) 
   {
       // Just example
       if (mb_strlen($newVal) > 4) {
           throw new InvalidArgumentException('...');
       }
       
       $this->anotherProperty = $newVal;
   }
}

Autocomplete

All these annotations fully work in the IDE, including autocomplete and highlighting incorrect behavior.

IDE Autocomplete And Highlighting

Type Hints

All properties with writeable behavior will be "type checkable".

/**
 * @property int|null $a
 */
class Some
{
    use Serafim\Properties\Properties;
    
    protected $a; 
}

//

$some = new Some;
$some->a = 23; // Ok
$some->a = null; // Ok
$some->a = 'string'; // Error: "TypeError: Value for property Some::$a must be of the type int|null, string given"

Primitive Type Hints

  • int (or integer) - property value are integer
  • bool (or boolean) - value are boolean
  • float (or double) - value are float
  • string - value are string or object with __toString method
  • null (or void) - value are nullable
  • resource - value are resource
  • object - value can be any object
  • mixed - no type checking
  • callable - value can be string, instance of \Closure, array with 2 args or object with __invoke method
  • scalar - value cannot be an object
  • countable - value can be a countable (array or object provided Countable interface).
  • self - value can be object of self class or string with name of self class

self keyword does not available yet: it will be supports in future

  • static - value can be instance of self class or string whos are sublass of self

static keyword does not available yet: it will be supports in future

  • $this - value can be only object instance of self class

$this keyword does not available yet: it will be supports in future

Arrays And Generics

  • array - value is type of array
  • Class[] - value is type of array or instance of \Traversable
  • scalar[] - value is type of array or instance of \Traversable
  • Collection<> - value is type of array or instance of "Collection" and \Traversable
  • Collection<T> - value is type of array or instance of "Collection" and \Traversable
  • Collection<T,V>- value is type of array or instance of "Collection" and \Traversable

Conjunction And Disjunction

  • a|b - means that the value must be type (a or b).
  • a&b - means that the value must be type (a and b).
  • a|b&c - means that the value must be type (a or (b and c)).

See more: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types

Production Mode

The code is quite effective, but in the production mode you should use caching. The package implements support for the PSR-16 standard.

$driver = new Psr16CacheDriver(); // Your PSR16 cache driver implementation 

$properties = Serafim\Properties\Bootstrap::getInstance();
$properties->setCacheDriver($driver);

properties's People

Contributors

lisennk avatar malezha avatar roquie avatar serafimarts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

properties's Issues

Возможность сбросить кеш, или автоматическая инвалидация по хешам/file modified time

Привет, похоже что не работает, когда класс имлементирует какой-либо интерфейс. Проблема была с кешом. Ниже описал в чём дело.

Мб стоит добавить метод для удаления кеша, или автоматическую инвалидацию?

Seems like tests are broken

Try to pull last version and run phpunit. I get the following output:

PHPUnit 5.2.9 by Sebastian Bergmann and contributors.

.....................EE..                                         25 / 25 (100%)

Time: 89 ms, Memory: 8.00Mb

There were 2 errors:

1) Serafim\Properties\Unit\WriteTypeHintsTestCase::testWritePrimitives
TypeError: Value for property Serafim\Properties\Unit\Stub\WritableTypeHints::objectValue must be of the type object, object given

/home/taras/work/github.com/Properties/src/Properties.php:90
/home/taras/work/github.com/Properties/tests/WriteTypeHintsTestCase.php:41

2) Serafim\Properties\Unit\WriteTypeHintsTestCase::testConditionalTypes
TypeError: Value for property Serafim\Properties\Unit\Stub\WritableTypeHints::arrayInstanceOfSelfValue must be of the type WritableTypeHints[], array given

/home/taras/work/github.com/Properties/src/Properties.php:90
/home/taras/work/github.com/Properties/tests/WriteTypeHintsTestCase.php:69

FAILURES!
Tests: 25, Assertions: 63, Errors: 2.

:)
must be of the type object, object given

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.