70 likes | 90 Views
There are always better versions of framework and programming languages available. However, making the most appropriate use of them is left to the part of developers. Understanding advance PHP Techniques becomes important when we know that there are many advanced techniques which are hardly used anywhere.
E N D
Understanding Advanced PHP Techniques: Rarely Used Tricks There are always better versions of framework and programming languages available. However, making the most appropriate use of them is left to the part of developers. Understanding advance PHP Techniques becomes important when we know that there are many advanced techniques which are hardly used anywhere. These techniques can be used in the most innovative manner. These may also turn out to be basics that may be extremely useful for you for the further projects you’d come across. Well, this is going to be all about those interesting advance techniques of PHP. Some Rarely Used Advance PHP Techniques Generally, developers always catch up with new and effective techniques in the industry. There are, however, many PHP tricks that are still not known or very rarely used. The major objective of this content is to throw light on those Here are the ones you may consider: Anonymous Functions and Classes You must sometimes consider the use of a callback function in your codes. There are many other options rather than a global function you can create a dynamic function instead which can be used once or twice in the code. Since naming is an
important task, this may save your time you’d spend on finding a correct name. Following is the example of using a call back function to sort people by their marks $list = [ ['name' => 'John', 'marks' => 31], ['name' => 'Mike', 'marks' => 25], ['name' => 'Ben', 'marks' => 27] ]; usort($list, function($a, $b) { if ($a['marks'] == $b['marks']) { return 0; } return ($a['marks'] < $b['marks']) ? -1 : 1; }); Further is an example of how a function is saved into a variable being dynamically created and then destroyed later. $sqrt = function($a) { return $a * $a; }; echo $sqrt(2); unset($sqrt); Anonymous classes attain optimum support with PHP 7. Therefore, this feature can be utilized in the most creative manner for advanced PHP programming. ALSO READ: CONVERT IOS APP TO ANDROID: A BASIC KNOW-HOW Convert Errors to Exceptions Prior to the arrival of PHP 7, error reporting had been a messy task. PHP 7 lets you work with probable improvements in that case. Well, you can use it to convert
errors to exceptions. All you need to do is add up a couple of lines more to your coding. Here’s an example: set_error_handler(function($errorNumber, $errorMessage, $errorFile, $errorLine) { throw new \ErrorException($errorMessage, 0, $errorNumber, $errorFile, $errorLine); }); But then, catching the exceptions is an important thing you would need to do. This is much easier than registering custom handlers, taking care of various errors handling configurations and suppressing errors that may occur at various stages. Take a glance at the example below: try { echo 5 / 0; } catch (Exception $e) { print_r($e); } Auto-loading Classes While writing object-oriented applications with PHP, creating one file for each class would be the best practice. The classes can be developed and maintained much more easily this way. Also, the application will load only those classes that are actually needed to complete a specific request. For this, you need to archive an autoload function in the class when needed. Instead of writing it like: include "some/dir/Class1.php" include "some/dir/Class2.php" include "some/dir/Class3.php" You may prefer writing: spl_autoload_register(function ($class) { include "some/dir/" . $class . ".php"; }); The code will then work perfectly well:
$object1 = new Class1(); $object2 = new Class2(); // Class3 will not load because it's not needed Magic Methods These days object-oriented programming is greatly considered. Well, understanding some advanced PHP techniques will help in the best use of them. This will help in taking your coding to a new level. Using magic methods you can tweak calls to methods of a class and also update the object state with the occurrence of a particular operation. Consider the below-mentioned class for example: class Student { public $age = null; public $Result = null; public $Status = null; } You can set the default values for the categories using the function _construct. Like below: class Student { public $age = null; public $Result = null; public $Status = null; function __construct(){ $this->age = 20; $this->Result = 'Pass'; $this->Status = 'Healthy'; } }
You may also use _set to validate properties and further retrieve them with _get. class Person { private $data = []; public $eyesColor = null; public $hairColor = null; function __set($name, $value){ if ($name === 'age') { if (is_int($value) && $value >= 18) { $this->data[$name] = $value; } else { throw new InvalidArgumentException('Age is invalid. Must be at least 18.'); } } } function __get($name) { return isset($this->data[$name]) ? $this->data[$name] : null; } function __isset($name) { return isset($this->data[$name]); } function __unset($name) { if (isset($this->data[$name])) { unset($this->data[$name]);} } } There is a long list of magic methods that you may refer.
XDebug When you have to work on large applications for several years, profiling and debugging will prove to be one of the peak requirements. Xdebug, in this case, had been one of the most useful tools and the best, advanced PHP topics to clarify. This makes it easier to identify and fix slow places in your code. ALSO READ: JAVA APPLICATIONS DEVELOPMENT: A SHORT TUTORIAL Command Line Apart from being one of the most popular server languages, PHP can also be utilized for writing programs and scripts which can be called via command line. Take a glance at the example: php hello-world.php -name John Mentioned below is the code of the program: if (isset($argv[1]) && $argv[1] === '-help') { echo 'Enter -name <your-name> so I can greet you properly.'; exit(); } if (isset($argv[1], $argv[2]) && $argv[1] === '-name') { echo 'Hello, ' . $argv[2]; exit(); } echo 'Invalid command. Type -help for help.'; exit(); register_shutdown_function() As the name says it all, this is the registration of a function that would be called when the execution of the considered request is near to the finish line. This can be used to print an output that is good enough or checks for fatal errors in the coding. You may also use it to log something.
register_shutdown_function(function() { $errorData = error_get_last(); if (is_array($errorData)) { ob_end_clean(); echo 'Error occured! - ' . $errorData['message']; } }); These are some of the rarely used advanced PHP techniques. That would surely prove to be extremely useful to you during your projects.