This may not be the answer anyone is looking for but Just to make sure why the code is not working I would like to inform that running CI3 on PHP>=8 is near impossible. Atleast it Is for me. If you have CI3 project, either downgrade your php version to less than 7 or upgrade your project to CI4. Even still there are a lot of tweaks to be performed on your code for it to work with CI4. But its better than fixing CI3 on php>8.
If anybody can provide a better answer I am willing to withdraw this as the selected answer.
That's all.