Compare commits

..

2 Commits

Author SHA1 Message Date
KJ
eff0b86762 Allow construct Neuron with named arguments.
Example:

<?php
$instance = new Neuron(stringValue: 'Hello world', integerValue: 50,
boolValue: false);

echo $instance->stringValue; // "Hello world" will be printed.
?>

Also, is possible to send infinite params without names and his names
will be numeric similar as a non-asociatie array, but as Neuron object.

Example:

<?php
$str = 'Hello world';
$int = 50;
$con = false;

$instance = new Neuron($str, $int);

echo $instance->{0}; // "Hello world" will be printed.
?>
2023-06-04 14:47:58 -04:00
KJ
39a1f9d85a Improve magic function __get.
Is not necessary another conditionals. When __get is called
is only when the the property is not defined, so
only need return null in order to avoid the
PHP notice of undefined property.
2023-06-04 14:34:24 -04:00

View File

@ -25,9 +25,15 @@ class Neuron {
/** /**
* __construct * __construct
* *
* @param object|array $data * @param array $data
*/ */
public function __construct(array|object $data = []) { 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) foreach($data as $key => $value)
$this->{$key} = $value; $this->{$key} = $value;
} }
@ -38,9 +44,8 @@ class Neuron {
* @param string $index * @param string $index
* @return mixed * @return mixed
*/ */
public function __get(string $index) { public function __get(string $index) : mixed {
return (isset($this->{$index}) && return null;
$this->{$index} != '') ? $this->{$index} : null;
} }
} }