New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

adhocore/get-in

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adhocore/get-in

Handy Traversal of chained objects with error trap and default value (suited for View).

Source
Packagist
This package is abandoned and is no longer maintained.
The author recommends using true instead.
This version has been yanked by its maintainers and should not be used.
Version
dev-master
Version published
Total downloads
13
Maintainers
1
Created
Source

about

this prevents php error "Call to a member function on a non-object" and provides elegant syntax to access the methods of chained object in a fluent way and also has default value for if it should fail at any point

rationale and usage

for something like:

echo $user->getGroup()->getPermission()->getName();

this scenario is not uncommon in any ORM. now, in case $user is null or $user->getGroup() is null or etc, it can be hell of a checks like:

$default = 'some name';
if ($user) {
    $group = $user->getGroup();
    if ($group) {
        $permission = $group->getPermission();
        if ($permission){
            echo $permission->getName();
        } else {
            echo $default;
        }
    } else {
        echo $default;
    }
} else {
    echo $default;
}

or it can be quite shorter in another smart way like:

$default = 'some name';
if ($user and 
    $group = $user->getGroup() and 
    $permission = $group->getPermission()
) {
    echo $permission->getName();
} else {
    echo $default;
}

still not perfect. adhocore/get-in saves one from this PITA by providing handy wrapper like:

echo \Ahc\Get::in($user, 'getGroup.getPermission.getName', 'some name');

installation

edit your composer.json to include "adhocore/get-in": "1.0.*@dev" in the require section and run composer update

advantage

  • prevents multi layer check
  • prevents errors like "Call to a member function on a non-object"
  • saves from temporary variable creation during the checks
  • provides a way to have default value should it fail at any point

the name

the name get-in is based on igorw/get-in which is similar thing for array manipulation, adhocore/get-in being for chained objects

Keywords

object

FAQs

Package last updated on 05 Aug 2018

Related posts