Home > Net >  After upgrade php version to 8, this error display
After upgrade php version to 8, this error display

Time:12-24

I am upgrading php 7 to php 8.0. In previous php version 7, this code working fine.

$child_parent['parent'][$resultData->parent_id]->child = 'Yes';

This above code working fine in php 7. Now I am upgading php version 8.0. In this new version it gives this error.

An uncaught Exception was encountered Type: Error

Message: Attempt to assign property "child" on null

code with details:-

$currentFile = $_SERVER["REQUEST_URI"];
    $parts = Explode('/', $currentFile);

    $currnet_menu = $parts[count($parts) - 1];

    $child_parent = array();
    $parent = array();
    $new_array = array();
    $increment = 0;
foreach ($menu_data as $key => $resultData) {
        //echo"<pre>";print_r($resultData); 
            //stdClass Object //output of $resultData
            //(
            //    [key_id] => 67
            //    [key_update_date] => 
            //    [key_update_by] => 
            //    [GroupModuleID] => 67
            //    [ModuleID] => 38
            //    [GroupId] => 1
            //    [ModuleName] => home
            //    [display_name] => Home Page
            //    [super_use] => 0
            //    [Mid] => 38
            //    [description] => 
            //   [parent_id] => 0
            //    [display_in_menu] => 1
            //    [module_icon] => home
            //    [groupName] => Super Admin
            //    [Uid] => 119
            //    [icon_html] => 
            //    [total_count] => 0
            //    [has_more_records] => 0
            )
        if ($resultData->display_in_menu == 1) {
            array_push($parent, $resultData->parent_id);
            if (in_array($resultData->parent_id, $parent)) {
                if ($resultData->parent_id == 0) {
                    $child_parent['parent'][$resultData->ModuleID] = $resultData;
                    $child_parent['parent'][$resultData->ModuleID]->child = 'No';
                } else {
                    $child_parent['child'][$resultData->parent_id][$increment] = $resultData;
                    $child_parent['parent'][$resultData->parent_id]->child = 'Yes';

                    $parts1 = Explode('/', $resultData->ModuleName);
                    $currnet_menu_check = $parts1[count($parts1) - 1];

                    if ($currnet_menu_check == $currnet_menu) {
                        $new_array[$resultData->parent_id]->open_child = 'Yes';
                    }
                }
                $increment  ;
            }
        }
    }

Thanks in advance.

CodePudding user response:

You can't just assign property on non object. This also errors on PHP 7 not just 8.

For example:

$arr = [];

$arr['parent'][1]->child = 'yes';

I'm trying to assign child property on non object. The $arr['parent'][1] is empty, not an object.
The result on PHP 7.0 - 7.4:

Warning: Creating default object from empty value

The result on PHP 8.0 :

Error: Attempt to assign property "child" on null

The difference between PHP 7.0 and PHP 8.0 is...
In PHP7.0 , The Warning level can still continue process
While in PHP 8.0 this becomes Error level and will be stopped the process.
If you show all errors or log all error level, you will see that this is not just happens on PHP 8.0 .

To make it work (to assign property), your variable must be object. In my example, the $arr['parent'][1] must be object.

$arr = [];

$arr['parent'][1] = new \stdClass();
$arr['parent'][1]->child = 'yes';

In your case, both $child_parent['parent'][$resultData->ModuleID] and $child_parent['parent'][$resultData->parent_id] needed to declare as an object before assign the property.

if ($resultData->parent_id == 0) {
    $child_parent['parent'][$resultData->ModuleID] = $resultData;// I think $resultData is already an object.
    $child_parent['parent'][$resultData->ModuleID]->child = 'No';
} else {
    $child_parent['child'][$resultData->parent_id][$increment] = $resultData;
    $child_parent['parent'][$resultData->parent_id] = new \stdClass();// added
    $child_parent['parent'][$resultData->parent_id]->child = 'Yes';

    // your code
}
  • Related