3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\Traits;
17
use ReflectionProperty;
20
* Trait PropertiesTrait
22
* Provides utilities for reading and writing
23
* class properties, primarily for limiting access
24
* to public properties.
29
* Attempts to set the values of public class properties.
33
final public function fill(array $params): self
35
foreach ($params as $key => $value) {
36
if (property_exists($this, $key)) {
37
$this->{$key} = $value;
45
* Get the public properties of the class and return as an array.
47
final public function getPublicProperties(): array
49
$worker = new class () {
50
public function getProperties(object $obj): array
52
return get_object_vars($obj);
56
return $worker->getProperties($this);
60
* Get the protected and private properties of the class and return as an array.
62
final public function getNonPublicProperties(): array
67
$reflection = new ReflectionClass($this);
69
foreach ($reflection->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED) as $property) {
70
if ($property->isStatic() || in_array($property->getName(), $exclude, true)) {
74
$property->setAccessible(true);
75
$properties[] = $property;