Code Monkey home page Code Monkey logo

commons's Introduction

Phramz/Commons Build Status

Commons is a php-library that comes with some handy utilities to ease your daily coding-business.

Install

It's easy if you use composer!

edit your composer.json

"require": {
    "phramz/commons": "*"
}

or via command line

php composer.phar require phramz/commons

Examples

Picture this ....

<?php

use Phramz\Commons\Property\PropertyUtils;

class Contact
{
    private $email = '[email protected]';
    private $phone = '123'

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getPhone()
    {
        return $this->phone;
    }

    public function setPhone($phone)
    {
        $this->phone = $phone;
    }
}

class User
{
    private $name = 'foo';
    private $contact = null;

    public function __construct()
    {
        $this->contact = new Contact();
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getContact()
    {
        return $this->contact;
    }

    public function setContact(Contact $contact)
    {
        $this->contact = $contact;
    }
}

// get an instance of our User class
$example = new User();

Now, maybe you want to access the private member name

// get an instance of PropertyUtils
$propertyUtils = new PropertyUtils();

$propertyUtils->getProperty('name', $example); // will return 'foo'

Wow! Not very exciting, as long as you could call getName() directly, right!? But what if you have an abritary object and you do not know exactly if there is a public method getName() or maybe name is just a public member and therefore no need for getter? The object may not even have a member by the name of name and implements ArrayAccess? The getProperty() - method can deal with all of these usecases.

Well, by the same way you can also set name to another value:

// get an instance of PropertyUtils
$propertyUtils = new PropertyUtils();

$propertyUtils->setProperty('name', 'bar', $example);
$propertyUtils->getProperty('name', $example); // will now return 'bar' ... as well as
$example->getName(); // ... will also return 'bar'

If you need to you can also access nested members like email at any depth by using the path seperator .

// get an instance of PropertyUtils
$propertyUtils = new PropertyUtils();

$propertyUtils->getProperty('contact.email', $example); // will return '[email protected]'

Do you need to deal with arrays? No problem at all:

// if our User-object were an array it would look like this
$example = array(
    'name' => 'foo',
    'contact' => array(
        'email' => '[email protected]',
        'phone' => '123'
    )
);

// get an instance of PropertyUtils
$propertyUtils = new PropertyUtils();

$propertyUtils->getProperty('name', $example); // will still return 'foo'
$propertyUtils->getProperty('contact.email', $example); // will still return '[email protected]'

There is one difference if you're working with arrays due these values are no references like objects. So if we want to set a new value to a member we need to save the manipulated array.

// get an instance of PropertyUtils
$propertyUtils = new PropertyUtils();

// setProperty() will return the manipulated array, so we write it back to $example
$example = $propertyUtils->setProperty('name', 'bar', $example);

$propertyUtils->getProperty('name', $example); // will return 'bar' ... as well as
$example->getName(); // ... will also return 'bar'

That's it! I hope this peace of software will be helpful! Have fun!

commons's People

Watchers

 avatar

Forkers

digitalkaoz

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.