PHP 8.1 introduces a new return type called ‘never’ that explicitely says that a method will never return a value. Similar to returning void, however after returning never you should guarantee that the program will either throw an exception or terminate itself with exit or die. If you fail to enforce the termination PHP will fail with TypeError exception. Let’s look at a few examples:
Continue reading “PHP return type ‘never’”Author: kikminev
Mysql: Delete a row when a foreign key constraint is not allowing it
Sometimes when you work in development mode you would like to delete a row but a foreign key constraint might prevent you from doing so. Imagine that you have a table customer and a related table order. It’s possible that you will have a foreign key constraint that prevents deleting orders of specific customer without using the delete cascade option(i.e. to delete the customer as well as the orders). If for some reason, for the sake of testing something in development you want to achive this you can use the FOREIGN_KEY_CHECKS flag and disable it until you delete your order. An example with customer and order will be:
SET FOREIGN_KEY_CHECKS=0;
UPDATE customer set order_id = null WHERE id = 21;
DELETE FROM `order` WHERE id = 1;
SET FOREIGN_KEY_CHECKS=1;
Running this query will actually allow you to delete the desired row despite having a foreign key constraing.
Include static image, javascript or other static resource in a Salesforce page
When developing Salesforce pages you might need to include your own images, javascript libraries or any other static resource. Doing this is easy but it first requires uploading the resource to Salesforce. You can do this by opening:
Salesforce Setup > Static Resouce > New
From there you need to upload the file and giving it a name that should be unique for the whole organisation. Also the max file size is 5 MB. Once the file is uploaded to your Salesforce you can include it in your template with the following coe:
<apex:includeScript value="{! $Resource.your_file_name }"/>
You just need to replace your_file_name with the name you have chosen at the time of the upload.
Symfony and Twig: how to check if flash messages are not empty
When rendering Symfony twig templates sometimes you might need to show one html element and hide another element depending if you have flash error/notice messages or not. For example if you have a page with registration form you might want to hide the form if it was submitted successfully and you only want to display a success message. For that you will need to check if the flash messages set by your controller are empty or not.
Continue reading “Symfony and Twig: how to check if flash messages are not empty”PHP spread operator
The so called spread operator was introduced in PHP 7.4 and it has rather peculiar usage. Given an array of elements it will take each element and spread it to it’s own placeholder element. E.g. the array:
Continue reading “PHP spread operator”Symfony get HTTP_X_FORWARDED_PROTO in controller
It’s often the case that people that use Cloudflare use the SSL option of cloudflare that secures the connection from the client to Cloudflare, but not from Cloudflare to the actual hosting server. In this case people might get the impression that the user is accessing the site from https but actually the hosting server is accessed with http and Symfony’s $request->isSecure() returns false.
Continue reading “Symfony get HTTP_X_FORWARDED_PROTO in controller”PHP Generators and yield
PHP generators allows you to iterate through data by using less memory compared to a standard foreach function. Simple example: If you want to display the numbers between 1 and 5000000 with a simple foreach you may hit the memory limits used by PHP.
Continue reading “PHP Generators and yield”