Blog
- Details
- Written by R. Elizondo
- Category: Symfony Framework
Keeping data protected and safe is key in modern Enterprise Software Applications. In PHP we have available the openSSL extension to be bundled with it1. When developing Symfony Applications you can follow this simple recipe to allow required data to be saved in encrypted format, and then decrypt it when need it:
Step 1. Make sure your Application has the openSSL extension declaring it as part your composer.json requirements:
Read more: How to encrypt and decrypt data in PHP Symfony 6+
- Details
- Written by R. Elizondo
- Category: Basics of Software Development
Autowiring in PHP refers to the ability to automatically resolve dependencies of a class without the need for manual instantiation or injection of dependencies.To implement autowiring in PHP using the ReflectionClass, you can follow these steps:
Step 1. Define the class that has dependencies that need to be autowired:
class MyTestingClass
{
/**
* @var DependencyClass
*/
private DependencyClass $dependency;
/**
* @param DependencyClass $dependency
*/
public function __construct(DependencyClass $dependency)
{
$this->dependency = $dependency;
}
}
Step 2. Create a method that uses ReflectionClass to get the constructor of the class and resolve its dependencies:
/**
* @throws ReflectionException
*/
function resolveDependencies($className)
{
$reflection = new ReflectionClass($className);
$constructor = $reflection->getConstructor();
$dependencies = [];
if ($constructor) {
foreach ($constructor->getParameters() as $param) {
$dependency = $param->getClass();
if ($dependency) {
//Use recursion to autowire nested dependencies
$dependencies[] = resolveDependencies($dependency->getName());
}
}
}
return $reflection->newInstanceArgs($dependencies);
}
Step 3. Call the resolveDependencies() function with the name of the class that needs autowiring:
$instance = resolveDependencies('MyTestingClass');
This will create an instance of ExampleClass with its dependency automatically resolved using autowiring. This approach assumes that all dependencies can be instantiated using their default constructor. If any of the dependencies require additional arguments to be passed to their constructor, you will need to modify the resolveDependencies() function accordingly.
ReflectionClass is very powerful tool and it can help you not only to autowire dependencies, but for validating data in a very easy way.
For more on this look into these posts:
How to validate input data in Symfony using DTOs
- Details
- Written by R. Elizondo
- Category: Basics of Software Development
In PHP, autowiring refers to a technique for automatically resolving dependencies when instantiating classes. To implement autowiring in PHP, you can use a dependency injection container, such as PHP-DI package, which provides autowiring out of the box.
Prerequisites:
- Vagrant Virtual Machine with CentOS 7, Nginx, MariaDb, NodeJs and PHP 8
- How to install Composer 2 in a Virtual Machine in 3 simple steps
Step 1. Install PHP-DI via composer:
composer require php-di/php-di
Step 2. Create a container instance and configure it to use autowiring:
use DI\ContainerBuilder;
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(true);
$container = $containerBuilder->build();
Step 3. Define your class and its dependencies:
class Foo {
public function __construct(Bar $bar) {
// ...
}
}
class Bar {
// ...
}
Step 4. Resolve the class using the container:
$foo = $container->get(Foo::class);
The container will automatically instantiate the Bar dependency and pass it to the constructor of Foo.
Autowiring can be limited to a specific namespace or package by using the useAnnotations(false) method of the container builder and annotating the classes with @Inject or @Autowired annotations. Additionally, it's important to note that autowiring can only work with classes that have type-hinted dependencies.
If you want to implement autowiring without any package: How to implement autowiring in php using reflexion class
- Details
- Written by R. Elizondo
- Category: Basics of Software Development
array_map(callable, array1,array2,array3,...) is a very powerful function available in PHP. Basically is a way to transform one or more arrays applying the logic in the callable function and output the result as another array. These are the most common ways to implement this function:
Option 1. Use a callable outside array_map function. Declare a function and call it inside the array_map function:
function processLetters($item)
{
switch ($item) {
case 'a':
$position = 'first';
break;
case 'b':
$position = 'second';
break;
case 'c':
$position = 'third';
break;
case 'd':
$position = 'fourth';
break;
case 'e':
$position = 'fifth';
break;
default:
$position = 'unknown position';
}
return 'This is ' . $position . ' in the ABC';
}
$array = ['a', 'b', 'c', 'e', 'f'];
$result = array_map('processLetters', $array);
Option 2. Use anonymous function inside the array_map function:
$array = ['a', 'b', 'c', 'e', 'f'];
$result = array_map(
function ($item): string {
switch ($item) {
case 'a':
$position = 'first';
break;
case 'b':
$position = 'second';
break;
case 'c':
$position = 'third';
break;
case 'd':
$position = 'fourth';
break;
case 'e':
$position = 'fifth';
break;
default:
$position = 'unknown position';
}
return 'This is ' . $position . ' in the ABC';
}, $array);
In both cases the output is this:
Array
(
[0] => This is first in the ABC
[1] => This is second in the ABC
[2] => This is third in the ABC
[3] => This is fifth in the ABC
[4] => This is unknown position in the ABC
)
Option 3. Use a Lambda Function inside array_map:
$array = ['a', 'b', 'c', 'e', 'f'];
$result = array_map(fn($letterInList): string => $letterInList . ' -- from ABC', $array);
The output is:
Array
(
[0] => a -- from ABC
[1] => b -- from ABC
[2] => c -- from ABC
[3] => e -- from ABC
[4] => f -- from ABC
)
Option 4. Use the elements of 2 or more arrays in the callable function:
/**
* @param string $letter
* @param string $position
* @return string
*/
function processLetters(string $letter, string $position): string
{
return 'This is letter: ' . $letter . ' is the ' . $position . ' in the ABC';
}
$letters = ['a', 'b', 'c', 'e', 'f'];
$positions = ['first', 'second', 'third', 'fourth', 'fifth'];
$result = array_map('processLetters', $letters, $positions);
Option 5. Use an associative array (also known as dictionary or map) to process keys and values in the callable function:
/**
* @param string $position
* @param string $letter
* @return string
*/
function processLetters(string $position, string $letter): string
{
return 'This is letter: ' . $letter . ' is the ' . $position . ' in the ABC';
}
$array = [
'first' => 'a',
'second' => 'b',
'third' => 'c',
'fourth' => 'd',
'fifth' => 'e'
];
$result = array_map('processLetters', array_keys($array), array_values($array));
In both cases the output is:
Array
(
[0] => This is letter: a is the first in the ABC
[1] => This is letter: b is the second in the ABC
[2] => This is letter: c is the third in the ABC
[3] => This is letter: d is the fourth in the ABC
[4] => This is letter: e is the fifth in the ABC
)
refer to array map function in PHP Documentation for more on this.
Page 11 of 42