PHP code example of lgse / php-object-builder

1. Go to this page and download the library: Download lgse/php-object-builder 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/ */

    

lgse / php-object-builder example snippets


use PHPOB\Model;
use PHPOB\ObjectBuilder;

/**
 * Example Object Class To Instantiate
 * Extending our `Model` class will add the `getInstance` static method
 * to your object so you don't have to create an object builder every
 * time you want to instantiate a class.
 */
class Customer extends Model {
    public function __construct(
        int $id,
        string $name,
        Address $address
    ) {
        $this->id = $id;
        $this->name = $name;
        $this->address = $address;
    }
    ...
}
class Address {
    public function __construct(
        string $street,
        string $city,
        string $state,
        int $zip
    ) {
        $this->street = $street;
        $this->city = $city;
        $this->state = $state;
        $this->zip = $zip;
    }
    ...
}

/**
 * Example instantiation using the `getInstance` static method
 */
$customer = Customer::getInstance([
    'id' => '0e2c0f21-2c46-4cf9-ad7e-2beeadb9282b',
    'name' => 'Microsoft',
    'address' => [
        'street' => '1 Microsoft Way',
        'city' => 'Redmond',
        'state' => 'WA',
        'zip' => 98052,
    ]
]);

/**
 * Example instantiation using the object builder
 * Note: You can pass in instantiated parameters that will be automatically passed through
 * to the object's constructor
 */
 $builder = new ObjectBuilder(Customer::class);
 $customer = $builder->getObject([
    'id' => '0e2c0f21-2c46-4cf9-ad7e-2beeadb9282b',
    'name' => 'Microsoft',
    'address' => new Address([
        'street' => '1 Microsoft Way',
        'city' => 'Redmond',
        'state' => 'WA',
        'zip' => 98052,
    ])
 ]);