Understanding Advanced PHP Techniques: Rarely Used Tricks - PowerPoint PPT Presentation

About This Presentation
Title:

Understanding Advanced PHP Techniques: Rarely Used Tricks

Description:

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. – PowerPoint PPT presentation

Number of Views:93
Slides: 8
Provided by: lesson91blogs

less

Transcript and Presenter's Notes

Title: Understanding Advanced PHP Techniques: Rarely Used Tricks


1
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 youd
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
2
important task, this may save your time youd
spend on finding a correct name. Following is
the example of using a call back function to sort
people by their marks
list 'name' gt 'John', 'marks' gt
31, 'name' gt 'Mike', 'marks' gt 25, 'name'
gt 'Ben', 'marks' gt 27 usort(list,
function(a, b) if (a'marks'
b'marks') return 0 return (a'marks'
lt 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
3
errors to exceptions. All you need to do is add
up a couple of lines more to your coding. Heres
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.ph
p"
You may prefer writing
spl_autoload_register(function (class)
include "some/dir/" . class . ".php" )
The code will then work perfectly well
4
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-gtage 20 this-gtResult
'Pass' this-gtStatus 'Healthy'
5
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 gt 18)
this-gtdataname value else throw
new InvalidArgumentException('Age is invalid.
Must be at least 18.') function
get(name) return isset(this-gtdataname) ?
this-gtdataname null function
isset(name) return isset(this-gtdataname)
function unset(name) if
(isset(this-gtdataname)) unset(this-gtdata
name)
There is a long list of magic methods that you
may refer.
6
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(argv1) argv1 '-help')
echo 'Enter -name ltyour-namegt so I can greet
you properly.' exit() if (isset(argv1,
argv2) argv1 '-name') echo 'Hello,
' . argv2 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.
7
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.
Write a Comment
User Comments (0)
About PowerShow.com