1. Go to this page and download the library: Download bapcat/propifier 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/ */
bapcat / propifier example snippets
class Foo {
public $a;
public $b;
}
class Foo {
private $a;
private $b;
public function __get($name) {
if(isset($this->$name)) {
return $this->$name;
}
throw new Exception('Invalid property!');
}
public function __set($name, $value) {
switch($name) {
case 'a':
$this->a = $value;
break;
case 'b':
throw new Exception('b is read-only!');
}
throw new Exception('Invalid property!');
}
}
...
private $array = [];
...
public function __get($name) {
if(isset($this->$name)) {
return $this->$name;
}
}
public function __set($name, $value) {
throw new Exception('You can\'t set me!');
}
...
$foo = new FooWithArrayThatCantBeSet();
$foo->array['a'] = 'Test'; // Note: no exception
echo $foo->array['a']; // -> 'Test'
class Foo {
private $a;
private $b;
private $array = [];
public function getA() {
return $this->a;
}
public function setA(A $a) {
$this->a = $a;
}
public function getB() {
return $this->b;
}
public function getOnlyOneArrayValue($index) {
return $this->array[$index];
}
}
$a = $foo->getA(); // rather than $foo->a
$one_array_value = $foo->getOnlyOneArrayValue(1); // rather than $foo->array[1]
class Foo {
use \BapCat\Propifier\PropifierTrait;
private $a;
private $b;
private $array = [];
public function __construct() {
$a = null;
$b = new B();
$array['test'] = 'Test';
}
protected function getA() {
return $this->a;
}
protected function setA(A $a) { // Type hinting
$this->a = $a;
}
protected function getB() {
return $this->b;
}
// Controlled access
//protected function setB(B $b) {
// $this->b = $b;
//}
// Propifier automatically detects arrays, and
// allows array access when using the property
protected function getArray($index) {
return $this->array[$index];
}
// You can even define iterators to add foreach support
protected function itrArray() {
return new ArrayIterator($this->array);
}
}
$foo = new Foo();
echo $foo->a; // -> null
$foo->a = new A(); // $a == new instance of A
echo $foo->b; // -> instance of B
$foo->b = new B(); // exception
echo $foo->array['test']; // -> 'Test'
$foo->array = []; // exception
$foo->array[1] = 'Test?'; // exception
foreach($foo->array as $key => $value) {
// ...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.