PHP Classes

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

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How PHP Developers Wi...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 55

Last month viewers: 2

Categories: PHP Tutorials, News

The PHP core developers recently approved a new request for a change to help PHP application developers to handle better mistakes in their code when they misspell the name of a class property in their code.

Read this short article to learn how this improves your process of detecting and fixing code that has mistakes so you can produce code of excellent quality as early as possible.




Loaded Article

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.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How PHP Developers Wi...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)