#Why Does This Happen?

Starting with PHP 8.2, the use of "parent" in callables is deprecated, which means the following are no longer supported:

'parent::method'
['parent', 'method']

If you're using either of these in your PHP code, it will throw the following error:

Deprecated: Use of "parent" in callables is deprecated

#How to Fix the Issue?

Fixing this issue is very straightforward, you can simply replace "parent" with parent::class. This means, you would use the following:

parent::class . '::method' // instead of 'parent::method'
[parent::class, 'method'] // instead of ['parent', 'method']

For example, the following code will result in the deprecation notice:

class BasicCalculator
{
	public function addOne(int $num): int
  {
    return $num + 1;
  }
}

class AdvancedCalculator extends BasicCalculator
{
  public function addOneToAll(array $nums): array
  {
    // Deprecated: Use of "parent" in callables is deprecated
    return array_map(['parent', 'addOne'], $nums);
  }
}

$calc = new AdvancedCalculator();
var_dump($calc->addOneToAll([1, 2, 3, 4]));

You can fix this, for example, in the following way:

class BasicCalculator
{
	public function addOne(int $num): int
  {
    return $num + 1;
  }
}

class AdvancedCalculator extends BasicCalculator
{
  public function addOneToAll(array $nums): array
  {
    return array_map([parent::class, 'addOne'], $nums);
  }
}

$calc = new AdvancedCalculator();
var_dump($calc->addOneToAll([1, 2, 3, 4])); // [2, 3, 4, 5]

Published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing.