2017 © Pedro Peláez
 

library property_access

Use calls methods as properties in PHP

image

teleiosis/property_access

Use calls methods as properties in PHP

  • Wednesday, October 25, 2017
  • by blbraner
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Synopsis

PropertyAccess is a small PHP package that will allow you to access your class methods as though there are properties. This works by utilizing __set, __get, and __isset magic methods. By using the above magic methods this also allows you to do this with non existent or private properties as well., (*1)

This is setup to mimic using Python's @property decorator, (*2)

Code Example

Lets say you have a User class that want to be able to set the firstName, lastName, and email on. You want to be able to automaticlly construct the email from the firstName.lastName@example.com Normally you would have something such as the example below, (*3)

<?php

class User
{

    public $firstName = '';
    public $lastName  = '';

    public function __constructor($firstName, $lastName)
    {
        $this->firstName =  $firstName;
        $this->lastName  =  $lastName;
    }

    public function email()
    {
        return "{$this->firstName}.{$this->lastName}@example.com";
    }    

}

$user = new User('John', 'Doe');
echo $user->email(); //John.Doe@example.com

However what if you want to be able to access the email address as a property? By annotating the method call with @property you can do just that., (*4)

<?php

//require composer autoloader
require 'vendor/autoload.php';

//include the trait
use teleiosis\PropertyAccess\traits\PropertyAccess;
class User
{
    use PropertyAccess;

    public $firstName = '';
    public $lastName  = '';

    public function __constructor($firstName, $lastName)
    {
        $this->firstName =  $firstName;
        $this->lastName  =  $lastName;
    }

    /**
    *@property
    */
    public function email()
    {
        return "{$this->firstName}.{$this->lastName}@example.com";
    }    

}

$user = new User('John', 'Doe');
echo $user->email; //John.Doe@example.com

You can also pass an array as arguments for your decorated methods, (*5)

<?php

//require composer autoloader
require 'vendor/autoload.php';

//include the trait
use teleiosis\PropertyAccess\traits\PropertyAccess;
class User
{
    use PropertyAccess;

    /**
    *@property
    */
    public function email($args = array())
    {
        return "{$args['firstName']}.{$args['lastName']}@example.com";
    }    

}

$user = new User();
$user->email = ['firstName' => 'John', 'lastName' => 'Doe'];
echo $user->email; //John.Doe@example.com

Installation

Install with composer, (*6)

composer require teleiosis/property_access

Tests

Tests are ran via PHPUnit, (*7)

Contributors

Brandon Braner, (*8)

The Versions

25/10 2017

dev-master

9999999-dev

Use calls methods as properties in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

by Brandon Braner