PHP code example of david-garcia / value-object

1. Go to this page and download the library: Download david-garcia/value-object library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

david-garcia / value-object example snippets




// Example for StringValue
use DavidGarcia\ValueObject\Primitive\StringValue;

StringValue::create('qwerty');

$stringValue1 = StringValue::create('qwerty', true);
$stringValue2 = StringValue::create('qwerty', true);

if ($stringValue1 === $stringValue2) {
    // TRUE
    // It's the same object, so it goes inside this conditional
}

$stringValue3 = StringValue::create('qwerty');
$stringValue4 = StringValue::create('qwerty');

if ($stringValue3 === $stringValue4) {
    // FALSE
    // Although they have the same value, the system has created two different objects
}

// `NullValue` does not expect any argument
$nullValue = NullValue::create();

$stringValue = StringValue::create('qwerty');
$stringValue->getValue(); // Returns 'qwerty'

$stringValue = StringValue::create('qwerty');
(string) $stringValue; // Returns 'qwerty'

$stringValue1 = StringValue::create('qwerty', true);
$stringValue2 = StringValue::create('qwerty', true);

if ($stringValue1->equals($stringValue2)) {
    // TRUE
    // We ignore the fact that is a cached object, as we compare the value
}

$stringValue3 = StringValue::create('qwerty');
$stringValue4 = StringValue::create('qwerty');

if ($stringValue3->equals($stringValue4)) {
    // TRUE
    // We have a match for the values, so even if we handle two different objects,
    // their values are equal
}