There is a project on YII2, which is hosted on the hosting and works without any problems. When deploying this project on the local server, the following error is popped up when the main page is displayed:

Mistake

The problem is that the non-static method is called, which in turn returns the result of the work of another static method.

public function findRedirectByUrl($url) { return self::findOne(['old_url' => $url]); } 

I read that this exception can be thrown out if, for example, the PHP version is changed to a newer one, this is somewhere in the description of switching to another version in the section "Changes that break backward compatibility". But the problem is clear, the question is whether it is possible, without correcting the code in the project, to ignore this error. In production, everything works with the PHP version the same as on the local server. Are there any parameters, such as php.ini, that can fix this?

  • In fact, it would be necessary to refactor the module code, but you can, for example, rest (new Redirects ()) -> instead of Redirects :: - fedornabilkin

1 answer 1

You can do this:

 $obj = new Redirects(); $redirects = $obj->findRedirectByUrl($url); 

if needed once, then you can "shorten" the record

 $redirects = (new Redirects())->findRedirectByUrl($url); 

But the problem is clear, the question is whether it is possible, without correcting the code in the project, to ignore this error.

In this case, you can, as long as the PHP version allows. Since the "inside" is still called the static method and the logic will not be violated. Well, on the "right" so of course you can not do

  • I did, but why should I create an object by changing the code, if everything works on production? I need to change the environment settings on LAN. How to do it? - John Roget
  • Such constructions still work, due to compatibility with older versions of PHP, but they give a warning (warning) that doing so is not good. If everything is working for you and you are not going to change it, then you can simply turn off error and warning messages d php.ini error_reporting = E_ALL & ~ E_NOTICE & ~ E_DEPRECATED & ~ E_STRICT & ~ E_WARNING - Dmitry Kozlov
  • As I use vagrant, I didn’t bother with his php.ini. Just added to index.php line: error_reporting (~ E_DEPRECATED); Of course, also editing the code, but does not take as much time as correcting all such conflicts in the project, for the sake of deployment on LAN. - John Roget
  • Just once on the hosting update the version of PHP, in which the warning for such actions will be replaced with error and you will have to shovel the whole project. It is better to clean the code as far as possible in advance - Dmitry Kozlov
  • Thanks for the recommendation, I think you are right, editing the project code is inevitable. - John Roget