Share
Preview
Let's talk about three advanced PHP functionalities that I think you will find useful for your work.
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
3 Advanced (and Useful) PHP Functionalities.
by Alex
Advanced PHP functionalities

Hey,
Alex here.

Let's talk about three advanced PHP functionalities that I think you will find useful for your work.

Of course, depending on your coding experience, you may already know about them and even find them easy to work with. In this case... that's good for you :)
But if you still don't know about them, this is a good chance to take a look at them.
I hope they can make your programming life easier and achieve better results.

Here we go.


1. Exceptions

Put simply: Exceptions help you handle application and function errors.
Now, if you never used them, you are probably asking yourself:

"But I already know how to handle errors. Do I really need to learn a different way?"

Well, here's the thing.

The simplest way to handle errors is by using return values. That is probably what you are doing right now.
For instance, many PHP functions return FALSE or NULL on error. So, all you need to do is to check their return values to see if an error occurred.

So, what's wrong with this method?

Let's say that you have a class method.
And this method calls another private method. And this private method calls other methods or functions.
For each function that can return an error value, you have to report that error all the way up to the caller.

That means that, for each function call, you have to check if its return value is the error value and, in that case, stop the execution and return an error value to the caller.
For example:

function func()
{
  $var1 = func1();
  if ($var1 === FALSE) {
    return FALSE;
  }
  $var2 = func2();
  if ($var2 === FALSE) {
    return FALSE;
  }
  //...
}

$var = func();
if ($var === FALSE) { /* Error */ }


This can make proper error reporting complex, and... error-prone.
Even worse, not all functions have the same error value.
For some functions FALSE is a perfectly valid return value, so they need to use NULL or -1 as the error value.


Exceptions solve this problem.
You only need to throw the Exception right when the error occurs, and you only need to check for it in the main code.
All the code between can simply ignore it, because the Exception will automatically reach the caller on its own.
For example:

function func()
{
   $var1 = func1();
   $var2 = func2();

   //...
}

try {
   $var = func();
}
catch (Exception $e) {
   echo 'Error: ' . $e->getMessage();
}


There is no need to reserve a special error return value, and there is no need to check all the return values of the whole call chain.
Exceptions also let you pass more information about the error (like a message and a numeric code) along with the error itself.
Exceptions also force you to catch them, so no errors can go undetected.

You can learn about PHP Exceptions here.


2. Namespaces

Namespaces are a way to organize your PHP code.

If you think of your functions and classes as files, then Namespaces are like directories.
Just like you can organize files into different directories and subdirectories, you can organize your functions, classes and constants into different Namespaces.

Using Namespaces let you:
  • Avoid name collisions, because each Namespace has its own name domain. This is especially handy if you are sharing your code.
  • Make your code more readable, because you can organize it logically.
  • Override some default PHP functions with the ones in your own Namespace, to better suit your project needs.

For example, take the PHPMailer library.
Its code is defined in the PHPMailer Namespace (and in sub-Namespaces as well), so there is no risk for it to use function names already used by PHP or by other libraries.

You can learn about PHP Namespaces here.


3. Traits (object-oriented programming)

The PHP OOP implementation provides few different tools.
One of such tools are Traits.

What are Traits?
Traits are similar to classes, but their purpose is not to be instantiated as objects.
Instead, Traits provide a common set of methods that you can use in different classes.
Therefore, Traits are a way to reuse methods across different classes.

When you include a Trait into a class, the class will inherit the Trait's properties and methods.
You can include more than one Trait, and you can resolve name conflicts with a specific syntax.

It's important to understand that Traits are meant to be used by different classes. A set of methods common to related-class are better implemented in a parent abstract class instead.

Here is a Trait example:

trait MyDebugger {
 
  public function debugSnapshot() { 
    //Get the class name and the object properties.
    $class = get_class($this);
    $vars = get_object_vars($this);
   
    //Then save them on the database.
  }
 
  public function clearSnapshots() {
     //Get the class name.
    $class = get_class($this);
   

    //Then clear the database $class snapshots.
}


This Trait provides a set of methods to save debugging snapshots of the current object and to remove all snapshots of the same class.
You can use these methods in every class by using the Trait, without any code duplication:

class MyClass() {
  use MyDebugger;
}
$myVar = new MyClass();
$myVar->debugSnapshot();


You can learn about PHP Traits here.



That's all for today.
Now it's your turn to send me a reply.
Did you know about these functionalities? What do you think about them?
Let me know if you have any questions!

Until next time,
Alex



Share the knowledge

Did you like this email? Share it with your friends and colleagues.
Click here to share it



You Don’t Want Your PHP Apps to be Hacked?
To make sure it will not happen, you need to know:
  • Which attacks you must prevent.
  • The specific defense techniques to stop each of those attacks.

PHP Security Mastery is my security course that will teach you exactly how to make your code secure.
Click here for the details.


Need help with your PHP code?
If you can't make your PHP code work, you can ask me for a Code Review.
I will verify your code, fix it and make sure it works the way you want.

Click here for the details.


Resources

Alex Web Develop - My blog where you can find my tutorials.
Alex PHP café
- My Facebook group where you can talk with me and other developers.

Infographic vector created by pikisuperstar - www.freepik.com




You are receiving this premium newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more email from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign