PHP code example of phine / accessor

1. Go to this page and download the library: Download phine/accessor 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/ */

    

phine / accessor example snippets


use Doctrine\ORM\Mapping as ORM;
use Phine\Accessor\AccessorTrait;
use Phine\Accessor\Type\InstanceType;

/**
 * This is an example Doctrine entity class.
 *
 * @ORM\Entity()
 * @ORM\Table()
 */
class Address
{
    use AccessorTrait;

    /**
     * The country the address resides in.
     *
     * @var Country
     *
     * @ORM\JoinColumn(name="country")
     * @ORM\ManyToOne(targetEntity="Country")
     */
    private $country;

    /**
     * The unique identifier for this address.
     *
     * @var integer
     *
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Id()
     */
    private $id;

    /**
     * The state the address resides in.
     *
     * @var State
     *
     * @ORM\JoinColumn(name="state")
     * @ORM\ManyToOne(targetEntity="State")
     */
    private $state;

    /**
     * Configures the accessors.
     */
    public function __construct()
    {
        $this->makePropertyAccessible('country');
        $this->makePropertyAccessible('id');
        $this->makePropertyAccessible('state');

        $this->makePropertyMutable('country', new InstanceType('Country'));
        $this->makePropertyMutable('state', new InstanceType('State'));
    }
}

// this all works just fine
$address = new Address();
$address->country = new Country();
$address->state = new State();

// these throw exceptions
$address->country = new State();
$address->id = 123;
$address->state = null;