A small package to add computed properties to any PHP class. 🐘

Overview

Computed Properties

GitHub Workflow Status (main) Total Downloads Latest Version License

This package provides a trait and attribute that can provide computed property support.

Installation

This package can be installed via Composer:

composer require ryangjchandler/computed-properties

Usage

Begin by adding the RyanChandler\Computed\Traits\WithComputedProperties trait to your class:

use RyanChandler\Computed\Traits\WithComputedProperties;

class Person
{
    use WithComputedProperties;

    public function getNameProperty()
    {
        return 'Ryan';
    }
}

You can then define a method using the get[name]Property naming conventions, where [name] is a pascal-cased version of your desired property name.

In the example above, we will be able to access the property name on the object.

$person = new Person;

echo $person->name; // 'Ryan'

Using Attributes

This package also provides a Computed attribute that allows you to use your own method names.

use RyanChandler\Computed\Traits\WithComputedProperties;
use RyanChandler\Computed\Attributes\Computed;

class Person
{
    use WithComputedProperties;

    public $firstName = 'Ryan';

    public $lastName = 'Chandler';

    #[Computed]
    public function fullName()
    {
        return $this->firstName . ' ' . $this->lastName;
    }
}

By default, Computed will let you access a property using the method name. In the example above, the property will be fullName.

$person = new Person;

echo $person->fullName; // 'Ryan Chandler'

If you would like to change the name of the computed property, you can pass a string to the attribute.

use RyanChandler\Computed\Traits\WithComputedProperties;
use RyanChandler\Computed\Attributes\Computed;

class Person
{
    use WithComputedProperties;

    public $firstName = 'Ryan';

    public $lastName = 'Chandler';

    #[Computed("name")]
    public function fullName()
    {
        return $this->firstName . ' ' . $this->lastName;
    }
}

You can now access the name property, which will run the Person::fullName() method.

$person = new Person;

echo $person->name; // 'Ryan Chandler'

Memoization

If you would like to only generate the value for a computed property once per request, you can add the RyanChandler\Computed\Attributes\Once attribute to your method.

use RyanChandler\Computed\Traits\WithComputedProperties;
use RyanChandler\Computed\Attributes\Once;

class Person
{
    use WithComputedProperties;

    #[Once]
    public function getRandProperty()
    {
        return rand(1, 10000);
    }
}

The random number will only be generated once per request. This is useful for expensive computations.

You might also like...
Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.
Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.

Laravel Blog Have you worked with Wordpress? Developers call this package wordpress-like laravel blog. Give our package a Star to support us ⭐ 😍 Inst

Simple PHP package for add two numbers

Sum Simple PHP package for add two numbers Installation To get the latest version of Sum, simply require the project using Composer: composer require

JsonQ is a simple, elegant PHP package to Query over any type of JSON Data

php-jsonq JsonQ is a simple, elegant PHP package to Query over any type of JSON Data. It'll make your life easier by giving the flavour of an ORM-like

Small convention based CQRS library for PHP

LiteCQRS for PHP Small naming-convention based CQRS library for PHP (loosely based on LiteCQRS for C#) that relies on the MessageBus, Command, EventSo

A small library to help run PHP servers easily and quickly.

PHP Server A small library to help run PHP servers easily and quickly. Installation composer require ahmard/php-server Usage PHP Built-In Server An i

This project processes a small database with php all on a web server. This project uses XAMPP to run the web server and the database.

PHP-introduction This project processes a small database with php all on a web server. This project uses XAMPP to run the web server and the database.

Small library providing some functional programming tools for PHP, based on Rambda

Functional library for PHP. Features: set of useful functions helpful in functional programming all functions are automatically curried every array ca

A small PHP library to generate YouTube-like ids from numbers.
A small PHP library to generate YouTube-like ids from numbers.

A small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.

Small Library to Serve Images in PHP in a Better Way (Resize, Compress) with Caching Support

A library for serving images and optimizing, changing their sizes, this library uses the caching feature, and in addition, it is very fast and small in size. In addition to these features, this library also optimizes images.

Releases(v0.2.0)
Owner
Ryan Chandler
Ryan Chandler
Small class to extract + compress .zip, .gz, .rar archives via browser.

The Unzipper The Unzipper extracts .zip and .rar archives or .gz/tar.gz files on webservers. It detects .zip/.rar/.tar.gz/.gz archives and let you cho

Lục Thiên Phong 10 Dec 24, 2022
PHPStan extension to support #[Readonly] constructor properties

icanhazstring/phpstan-readonly-property Support #[Readonly] promoted constructor properties for PHPStan. This library is used to have a full transitio

Andreas Frömer 4 Apr 5, 2022
Validated properties in PHP8.1 and above using attribute rules

PHP Validated Properties Add Rule attributes to your model properties to make sure they are valid. Why this package? When validating external data com

null 23 Oct 18, 2022
Joole Reflector - used to work with the properties of objects, their changes and merges

Joole Reflector allows you to change protected, as well as private properties of an object.

Ravil Sitdikov 1 May 7, 2022
[READ-ONLY] Properties define model metadata.

Charcoal Property Properties define object's metadata. They provide the building blocks of the Model's definition. Properties are defined globally for

The Charcoal PHP Framework 0 Jun 21, 2022
A robust and flexible way to add double-opt-in (DOI) to any form in Mautic

Mautic double-opt-in (DOI) plugin Adds a robust and flexible way to add a double-opt-in process (DOI) to any form in Mautic. What is the plugin for? I

Content Optimizer GmbH 11 Dec 29, 2022
Exploiting and fixing security vulnerabilities of an old version of E-Class. Project implemented as part of the class YS13 Cyber-Security.

Open eClass 2.3 Development of XSS, CSRF, SQLi, RFI attacks/defences of an older,vulnerable version of eclass. Project implemented as part of the clas

Aristi_Papastavrou 11 Apr 23, 2022
Track any ip address with IP-Tracer. IP-Tracer is developed for Linux and Termux. you can retrieve any ip address information using IP-Tracer.

IP-Tracer is used to track an ip address. IP-Tracer is developed for Termux and Linux based systems. you can easily retrieve ip address information using IP-Tracer. IP-Tracer use ip-api to track ip address.

Rajkumar Dusad 1.2k Jan 4, 2023
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022