try_these ( $attrs, $base, $default )

Returns the first value that is set. Based on Try.these() from Prototype.

Access

public

Parameters

Parameter

Type

Required

Description

$attrs

array

Required

The attributes to test, as strings. Intended for testing properties of the $base object, but also works with variables if you place an @ symbol at the beginning of the command.

$base

object

Optional

The base object to use, if any.

$default

mixed

Optional

What to return if there are no matches. Defaults to null.

Returns

Type

Description

mixed

Either a matching property of a given object, boolean false, or any other data type you might choose.

Examples

Return the first non-falsey value.

// Test data
$obj = new StdClass;
$obj->a = null;
$obj->b = null;
$obj->c = 1;
$obj->d = null;

// Instantiate
$s3 = new AmazonS3();

// Success?
var_dump($s3->util->try_these(array('a', 'b', 'c', 'd'), $obj, true));
Result:
int(1)

Source

Method defined in utilities/utilities.class.php | Toggle source view (25 lines) | View on GitHub

public function try_these($attrs, $base = null, $default = null)
{
    if ($base)
    {
        foreach ($attrs as $attr)
        {
            if (isset($base->$attr))
            {
                return $base->$attr;
            }
        }
    }
    else
    {
        foreach ($attrs as $attr)
        {
            if (isset($attr))
            {
                return $attr;
            }
        }
    }

    return $default;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback