PHP code example of grottopress / getter
1. Go to this page and download the library: Download grottopress/getter 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/ */
grottopress / getter example snippets
declare (strict_types = 1);
namespace Vendor;
use GrottoPress\Getter\GetterTrait;
class MyClass
{
/**
* Import trait
*/
use GetterTrait;
private $haveMe;
private $leaveMeAlone;
public function __construct()
{
$this->haveMe = 'Hello :-)';
$this->leaveMeAlone = 'Go away!';
}
/**
* Define your private getter method
* Method name should be of the format "get{$attrName}"
*/
private function getHaveMe(): string
{
return $this->haveMe;
}
// ...
}
// Instantiate
$object = new Vendor\MyClass();
// Try to get attributes
echo $object->haveMe; // => Hello :-)
echo $object->leaveMeAlone; // => Error: 'getLeaveMeAlone()' not defined
echo $object->nonExistent; // => Exception: 'nonExistent' does not exist