PHP code example of mfonte / propaccessor

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

    

mfonte / propaccessor example snippets


use Mfonte\PropAccessor\PropifierTrait;

class User {
    use PropifierTrait;

    private string $name;

    public function setName(string $name): void {
        $this->name = ucfirst($name);
    }

    public function getName(): string {
        return $this->name;
    }
}

$user = new User();
$user->name = 'john';
echo $user->name; // Outputs 'John'

class FeatureToggle {
    use PropifierTrait;

    private bool $isEnabled = false;

    public function isEnabled(): bool {
        return $this->isEnabled;
    }

    public function setEnabled(bool $value): void {
        $this->isEnabled = $value;
    }
}

$feature = new FeatureToggle();
$feature->enabled = true;

if ($feature->enabled) {
    // Feature is enabled
}

use ArrayIterator;

class Collection {
    use PropifierTrait;

    private array $items = [];

    public function setItems(int $index, mixed $value): void {
        $this->items[$index] = $value;
    }

    public function getItems(int $index): mixed {
        return $this->items[$index] ?? null;
    }

    public function itrItems(): ArrayIterator {
        return new ArrayIterator($this->items);
    }
}

$collection = new Collection();
$collection->items[0] = 'Item 1';
$collection->items[1] = 'Item 2';

foreach ($collection->items as $index => $item) {
    echo "$index: $item\n";
}

class Config {
    use PropifierTrait;

    protected static array $propertyMap = [
        'dbHost' => ['get' => 'getDatabaseHost', 'set' => 'setDatabaseHost'],
    ];

    private string $databaseHost;

    protected function getDatabaseHost(): string {
        return $this->databaseHost;
    }

    protected function setDatabaseHost(string $host): void {
        $this->databaseHost = $host;
    }
}

$config = new Config();
$config->dbHost = 'localhost';
echo $config->dbHost; // Outputs 'localhost'