13

How PHP Developers Will be Able to Handle Better Undefined Properties in PHP 9.0

 1 year ago
source link: https://www.phpclasses.org/blog/post/1131-How-PHP-Developers-Will-be-Able-to-Handle-Better-Undefined-Properties-in-PHP-90.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

In this article, you will learn about the following:

1. What Happens In Current PHP Versions When a PHP Developer Misspells a Class Property

2. Why You Should Detect and Fix Misspelling Mistakes as Early As Possible

3. How PHP 9.0 Will Help You To Improve the Detection of Misspelling Mistakes

4. How Can You Detect the Misspelling Mistakes in Current PHP Versions

1. What Happens In Current PHP Versions When a PHP Developer Misspells a Class Property

PHP has always been a very tolerant language to developer mistakes. On the good side, this is good because developers can test their code very quickly. On the bad side, this circumstance allows PHP developers to write code that may have mistakes when it ships to the production environment.

One common mistake is to misspell the name of a class property, as you can see in the example below:

class test
{
    public $variable;
}

$object = new test;

$object->varaible = 'Hello world!';

echo $object->variable;

Although the syntax of the code is correct, the name of the class property named variable is misspelled when the code tries to assign a value.

This leads to a situation where the class property named variable is treated as if the code never assigned it. So its value remains as if it was given to null.

Before PHP 8.0, the PHP runtime engine would have an E_NOTICE error. Since PHP 8.0, the error would be E_WARNING.

PHP continues to execute the code because the error is not fatal. 

2. Why You Should Detect and Fix Misspelling Mistakes as Early As Possible

Any developer mistake should be fixed as soon as possible because each error may cause your application not to work well.

If an application does not work well, the users may end up not using the application.

If the users do not use the application, the application owner will not be happy. Suppose you are the application owner or a customer who pays you to develop the application and realizes that the application is unsuccessful. In that case, you may stop working on the application.

So the sooner you fix any mistakes, the better for you, your business, and your users.

A mistake caused by misspelling may go unnoticed for a long time if you do not have a method to detect the consequences of this kind of mistake.

3. How PHP 9.0 Will Help You To Improve the Detection of Misspelling Mistakes

A misspelling mistake that causes an attempt to access an undefined property will trigger an error exception in PHP 9.0, according to an RFC (Request For Change) document that the PHP core developers recently accepted.

This means a PHP script with this kind of mistake will trigger an exception when the code with the error is executed. If the PHP application with this kind of mistake does not catch the exception, the PHP code execution will end with a fatal error.

This way, PHP developers can detect this kind of error more easily in PHP 9.0 because fatal mistakes lead to blank pages when PHP is used to generate the content of the pages.

This is better than leaving the application running with an unnoticed error because developers may be able to detect this kind of error earlier when they are running their application in the development environment.

4. How Can You Detect the Misspelling Mistakes in Current PHP Versions

One way to detect misspellings and other mistakes is to enable and monitor the PHP error log.

The error log can be monitored in any environment, regardless of the production, testing, or development environment.

I use a log watcher class I developed to monitor log files and email any changes. This way, I can get email messages as soon as errors happen in the environment where PHP is executed.

This solution to detect any mistakes in PHP code that lead to notices in the PHP error log works well in all versions of PHP.

You can learn more about implementing this solution by reading this article in the log watcher package blog about 10 Steps to properly do PHP Bug Tracking and Fixing as Fast as possible.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK