60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Libs;
|
|
|
|
use AllowDynamicProperties;
|
|
|
|
/**
|
|
* Neuron - DuckBrain
|
|
*
|
|
* Neuron, serves to create an object that will hold values.
|
|
* In addition, it has the special characteristic that when trying
|
|
* to access an undefined property, it will return null instead
|
|
* of generating a notice (PHP notice) for an undefined variable or property.
|
|
*
|
|
* The constructor accepts an object or an array containing the
|
|
* values that will be defined.
|
|
*
|
|
* @author KJ
|
|
* @website https://kj2.me
|
|
* @license MIT
|
|
*/
|
|
#[AllowDynamicProperties]
|
|
class Neuron
|
|
{
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param array $data Data to initialize the Neuron with. Can be an array or an object.
|
|
*/
|
|
public function __construct(...$data)
|
|
{
|
|
if (
|
|
count($data) === 1 &&
|
|
isset($data[0]) &&
|
|
(is_array($data[0]) ||
|
|
is_object($data[0]))
|
|
) {
|
|
$data = $data[0];
|
|
}
|
|
|
|
foreach ($data as $key => $value) {
|
|
$this->{$key} = $value;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Magic method __get
|
|
*
|
|
* This method is called when an undefined property is accessed.
|
|
* It returns null instead of triggering an E_NOTICE.
|
|
*
|
|
* @param string $index The name of the property being accessed.
|
|
* @return null Always returns null for undefined properties.
|
|
*/
|
|
public function __get(string $index): null
|
|
{
|
|
return null;
|
|
}
|
|
}
|