-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6542aef
commit d04ced9
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters