PHP code example of himanshu-m / accessors

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

    

himanshu-m / accessors example snippets


use Accessors;

class AccessorsTest
{
    use Accessors;

    private string $prop1;
    private string $prop2;

    function __construct(string $p1, string $p2)
    {
	$this->prop1 = $p1;
	$this->prop2 = $p2;

	$this->acccessible("prop1");
	// prop2 can also be renamed as readonlyProp for access from outside.
	$this->readonly("readonlyProp", "prop2");
    }
}

$obj = new AccessorsTest("property1", "property2");

echo $obj->prop1;
// property1
$obj->prop1 = "changed prop1";
echo $obj->readonlyProp;
// property2
$obj->readonlyProp = "cannot change prop2...";
// Exception: Property 'readonlyProp' of class AccessorsTest is not accessible