1. Go to this page and download the library: Download webinarium/php-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/ */
webinarium / php-properties example snippets
class User
{
protected int $id;
protected string $firstName;
protected string $lastName;
public function getId(): int
{
return $this->id;
}
public function getFirstName(): string
{
return $this->firstName;
}
public function setFirstName(string $firstName)
{
$this->firstName = $firstName;
}
public function getLastName(): string
{
return $this->lastName;
}
public function setLastName(string $lastName)
{
$this->lastName = $lastName;
}
public function getFullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
}
/**
* @property-read int $id
* @property string $firstName
* @property string $lastName
* @property-read string $fullName
*/
class User
{
protected int $id;
protected string $firstName;
protected string $lastName;
public function __isset($name)
{
if ($name === 'fullName') {
return true;
}
return property_exists($this, $name);
}
public function __get($name)
{
if ($name === 'fullName') {
return $this->firstName . ' ' . $this->lastName;
}
return property_exists($this, $name)
? $this->$name
: null;
}
public function __set($name, $value)
{
if ($name === 'id') {
return;
}
if (property_exists($this, $name)) {
$this->$name = $value;
}
}
}
/**
* @property-read int $id
* @property string $firstName
* @property string $lastName
* @property-read string $fullName
*/
class User
{
use PropertyTrait;
protected int $id;
protected string $firstName;
protected string $lastName;
protected function getters(): array
{
return [
'fullName' => fn (): string => $this->firstName . ' ' . $this->lastName,
];
}
}