Skip to content

Commit

Permalink
improve reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
nrenvoise-ubitransport committed Jul 2, 2021
1 parent 6542aef commit d04ced9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions break/break.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ function tryBreak(array $items)
'name' => 'apple',
'break' => true
]
],
[
'name' => 'jean saisdavantage',
'break' => false,
'fruit' => [
'name' => 'apple',
'break' => true
]
]
]);
29 changes: 29 additions & 0 deletions empty_vs_isset_vs_boolval/empty_vs_isset_vs_boolval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

$fruits = [
'apple' => 3,
'pear' => [],
'cherry' => null
];

function compare(array $items, string $key)
{
@$results = [
'empty()' => empty($items[$key]),
'isset() && boolval()' => isset($items[$key]) && boolval($items[$key]),
'isset()' => isset($items[$key]),
'boolval()' => boolval($items[$key]),
"null === $key" => null === $items[$key]
];

echo "\n$key:\n";

foreach ($results as $key => $value) {
echo " $key: ".var_export($value, true)."\n";
}
}

compare($fruits, 'hello');
compare($fruits, 'apple');
compare($fruits, 'pear');
compare($fruits, 'cherry');
44 changes: 44 additions & 0 deletions reflection/ReflectionTrait/ReflectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

trait ReflectionTrait
{
/**
* Call a private method of an object.
*
* @param mixed $arguments
*
* @return mixed
*/
protected function callPrivateMethod(object $object, string $method, ...$arguments)
{
$reflectionMethod = new \ReflectionMethod($object, $method);
$reflectionMethod->setAccessible(true);

return $reflectionMethod->invokeArgs($object, $arguments);
}

/**
* Set a private property.
*
* @param mixed $value
*/
protected function setPrivateProperty(object $object, string $property, $value): void
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value);
}

/**
* Get a private property.
*
* @return mixed
*/
protected function getPrivateProperty(object $object, string $property)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);

return $reflectionProperty->getValue($object);
}
}
File renamed without changes.
11 changes: 10 additions & 1 deletion regex/testRegex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ function testRegex(string $regex, array $testValues)
$result = preg_match($regex, $testValue) ? "Correct" : "Wrong";
echo "$result: $testValue\n";
}
}
}

testRegex(
'#^http://.+\.local#',
[
'http://2cloud.local',
'http://2cloud.zog.local',
'http://2cloud.app'
]
);

0 comments on commit d04ced9

Please sign in to comment.