Home>Article>Backend Development> Two methods of passing PHP implementation classes as parameters
When working on PHP projects, it is often necessary to dynamically use the same method name of a certain class. For example, class A has a get method, and class B also has a get method. At this time, there are only two classes that are very easy to solve. It can be solved perfectly with an if. What if there are N such classes? Then you need the method I will use later to achieve it!
In fact, I only discovered this trick when I was looking at the ThinkPHP framework. That is, PHP can replace a class by its name or its space name. In this case, you can directly pass the class name or the space name of the class to realize the function of passing the class as a parameter.
test(); } }class B{ public function test(){ var_dump('class B'); } }$a = new A();$a->test();//最后将输出class B
//FileAtest(); } }$a = new A();$a->test();
//FileBFinal output: testB\
The above is the detailed content of Two methods of passing PHP implementation classes as parameters. For more information, please follow other related articles on the PHP Chinese website!