PHP code example of avantar / readable-only-properties

1. Go to this page and download the library: Download avantar/readable-only-properties 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/ */

    

avantar / readable-only-properties example snippets




class User
{
    use ReadableOnlyProperties;

    private string $name;
    private int $age;

    public function __construct(string $name, int $age)
    {
        $this->name = $name;
        $this->age = $age;
    }
}

$user = new User('Chris', 35);
echo $user->name;

$user->name = 'Charlie';

Fatal error: Uncaught Exception: Property name is read only in /Users/avantar/Projects/readable-only-properties/ReadableOnlyProperties.php:18
Stack trace:
#0 /Users/avantar/Projects/readable-only-properties/ReadableOnlyProperties.php(42): User->__set('name', 'Charlie')
#1 {main}
  thrown in /Users/avantar/Projects/readable-only-properties/ReadableOnlyProperties.php on line 18