Home  >  Article  >  Backend Development  >  Using callback functions in php classes

Using callback functions in php classes

怪我咯
怪我咯Original
2017-06-19 13:22:552314browse

Recently learning PHPObject-orientedProgramming, I feel that object-oriented thinking does play a huge role in solving complex problems. I can’t imagine if the StarCraft game was developed How complicated it is without using object-oriented thinking. Object-oriented thinking is indeed a major progress in the history of computer programming methods.

Today I want to call another class method as its callback function in the class method I created. I wanted to write the name of another class method directly in the place of the callback function. However, I kept getting an error saying "the parameter is invalid". Then I thought about whether I should try using the SELF keyword, but I still got this error. I really don’t know what to do. I checked the Chinese manual and couldn’t find a solution. Then I carefully looked at the manual on the PHP official website. This manual has the syntax to solve this problem.

class utils{

 

   public function array_or( $input )

   {

      return array_reduce( $input, array('Utils','sum' ), 0 );

   }

   public function sum( $v, $w )

   {

      return $v += $w;

   }

 

}

$uti = new utils;

$a = array(1,2,3,4,5);

echo $uti->array_or($a);

makes me feel very strange. Why is calling another class method in the form of array? It seems that using the SELF keyword is more appropriate. Hey, I really don’t know what they are doing. What considerations go into designing such a grammar. But I think there must be a reason for the design like this, but I haven't realized this reason yet.

The above is the detailed content of Using callback functions in php classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn