Home  >  Article  >  Backend Development  >  Detailed explanation of dynamic instantiation objects in the PHP framework

Detailed explanation of dynamic instantiation objects in the PHP framework

伊谢尔伦
伊谢尔伦Original
2017-06-26 14:09:322735browse

In framework development, modular development, etc., we may have a need to dynamically instantiate objects when PHP is running.

What is a dynamic instantiation object? Let's first take a look at the concept of a variable function (Variable function) in PHP. For example, the following code:

function foo() {
    echo 'This is the foo function';
}
$bar = 'foo';
$bar();

Running the above code will output "This is the foo function". For details, please refer to the PHP manual: Variable functions. Of course, if you need to call dynamically, then use the call_user_func or call_user_func_array function. The usage of these two functions is not the focus of this article. If you don't understand, please check other information. Back to the topic of this article: What is a dynamically instantiated object? I believe that dynamic instantiation of objects means that the objects that need to be instantiated are dynamically determined (determined by variables) during the run-time of the program (determined by variables), rather than being written directly in the code.

Through the above examples, we already know how to dynamically call a function at runtime. Today, when object orientation is so popular, in some codes, we need to dynamically instantiate a class. What should we do? What to do?

Situation 1: The constructor of the class has no parameters or the number of parameters is determined

If the constructor of the class has no parameters or the class we want to instantiate does not have any parameters at all Without a constructor, it seems simpler. You can change it according to the above example. Well, follow the same example. Who doesn't know:

Code example: (Constructor has no parameters)

class FOO {
    private $a, $b;
    public function construct() {
        $this->a = 1;
        $this->b = 2;
    }
    public function test() {
        echo &#39;This is the method test of class FOO<br />&#39;;
        echo &#39;$this->a=&#39;, $this->a, &#39;, $this->b=&#39;, $this->b;
    }
}
$bar = &#39;FOO&#39;;
$foo = new $bar();
$foo->test();

Run it and you will see the following output:

This is the method test of class FOO
$this->a=1, $this->b=2

Well, if we want to pass parameters, then let’s do this:

class FOO {
    private $a, $b;
    public function construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }
    public function test() {
        echo &#39;This is the method test of class FOO<br />&#39;;
        echo &#39;$this->a=&#39;, $this->a, &#39;, $this->b=&#39;, $this->b;
    }
}
$bar = &#39;FOO&#39;;
$foo = new $bar(&#39;test&#39;, 5);
$foo->test();

You can also get similar results:

This is the method test of class FOO
$this->a=test, $this->b=5

Very ideal.

Situation 2: Class constructionParameters of the functionThe number is uncertain

This situation will be a lot more troublesome, but if you want to write it more universally, just This situation has to be considered. For example, we have the following two classes

class FOO {
    public function test() {
        echo &#39;This is the method test of class FOO&#39;;
    }
}
class BAR {
    private $a, $b;
    public function construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }
    public function test() {
        echo &#39;This is the method test of class BAR<br />&#39;;
        echo &#39;$this->a=&#39;, $this->a, &#39;, $this->b=&#39;, $this->b;
    }
}

We want a common way to instantiate these two classes. We noticed that the FOO class does not have a constructor, or it can be considered that the number of parameters of the FOO class's constructor is zero; while the BAR class's constructor has parameters. Fortunately, PHP5 is powerful enough and introduces the concept of reflection. For details, please refer to the PHP manual: Reflection, although there is nothing to refer to in the manual :). Fortunately, the naming is well written. You can already get a rough idea from the class name and method name, so you don’t need too many words.

Okay, let’s use the reflection of PHP5 to start this matter:

(Students who are still using PHP4, please don’t go away. If you have a PHP version without reflection or Is it because of compatibility or because you don’t want to upgrade? Anyway, you don’t want to use reflection. There is a solution below)

$class = new ReflectionClass(&#39;FOO&#39;);
$foo = $class->newInstance(); //或者是$foo = $class->newInstanceArgs();
$foo->test();

Did you see anything? Next:

$class = new ReflectionClass(&#39;BAR&#39;);
$bar = $class->newInstanceArgs(array(55, 65));
$bar->test();

OK, it seems OK, so let’s sort it out and create a general function. We want to design it like this. The first function of this function is the name of the class to be instantiated. From the second The first parameter is the parameter of the constructor of the class to be instantiated. If there are several, write them down. If not, don't write them down. To implement a function with a variable number of parameters, we have two methods:

The first is a method similar to:

function foo($arg1, $arg2 = 123, $arg3 = &#39;test&#39;, $arg4 = null, ....... ) {
    //some code;
}

. This method has two disadvantages. First The first is if you need to pass 100 parameters, should you just write 100 parameters? The second is that you have to determine which parameter in the program is null or other default value. (Digression: The default value of the parameter in this way of writing must be placed at the end. You cannot insert a parameter without a default value in the middle or in front of a parameter with a default value. Otherwise, you must also explicitly write the parameter with a default value when you call it. value)

Another way to implement a variable number of parameters is to use PHP's built-in function func_get_args (click here to read the manual) in the function to obtain the parameters passed to the function. Similar functions include func_get_num and func_get_arg. Forget it, I'm lazy. You can find the manual and read it yourself.

Then, it seems to be much more convenient to use this function. Based on our imagined arrangement of function parameters , the code should look like this:

function newInstance() {
    $arguments = func_get_args();
    $className = array_shift($arguments);
    $class = new ReflectionClass($className);
    return $class->newInstanceArgs($arguments);
}

OK, let’s do it Take a look at the effect:

$foo = newInstance(&#39;FOO&#39;);
$foo->test();
//输出结果:
//This is the method test of class FOO
$bar = newInstance(&#39;BAR&#39;, 3, 5);
$bar->test();
//输出结果:
//This is the method test of class BAR
//$this->a=3, $this->b=5

Just four lines of code, the effect is quite perfect. Then, if applied to a class, we can use this idea and write it directly as Magic Method, which can make our class cooler!

class INSTANCE {
    function call($className, $arguments) {
        $class = new ReflectionClass($className);
        return $class->newInstanceArgs($arguments);
    }
}
$inst = new INSTANCE();
$foo = $inst->foo();
$foo->test();
//输出结果:
//This is the method test of class FOO
$bar = $inst->bar(&#39;arg1&#39;, &#39;arg2&#39;);
$bar->test();
//输出结果:
//This is the method test of class BAR
//$this->a=3, $this->b=5

Kaka, feel good.

Next, let’s discuss the situation without using reflection classes. For example, there is no reflection in PHP4, and some old projects run on PHP4. Or if you want to ensure the project's compatibility with unknown environments, whatever, let's take care of how to dynamically transfer parameters. There is only one function for dynamic parameter transfer in PHP: call_user_func_array (click here to view the manual). This is a function that dynamically calls a function. Its function is to pass the parameters of the function to the function to be called in the form of an array. Well, I was confused myself, so let’s look directly at the example:

function foo($a, $b) {
    echo &#39;$a=&#39;, $a, &#39;<br />&#39;;
    echo &#39;$b=&#39;, $b;
}
call_user_func_array(&#39;foo&#39;, array(1, &#39;string&#39;));
//本例输出结果:
//$a=1
//$b=string

那么,要实现用这种方法来动态实例化对象并传参,呃……,只有曲线救国了,我们得先写一个函数,让这个函数来实例化对象,而这个函数的参数就原原本本地传给要实例化对象的类的构造函数就好了。打住!那这个函数得有几个参数啊?怎么实现传递不同个数的参数呢?嘿嘿,我一声冷笑,你忘了PHP里提供一个创建匿名函数的函数吗?(又开始绕起来了……)create_function(手册在此),照着手册里面的例子直接画就可以了,我也懒得打字了,直接看下面的代码,注释我写清楚点大家都明白了:

function newInst() {
    //取得所有参数
    $arguments = func_get_args();
    //弹出第一个参数,这是类名,剩下的都是要传给实例化类的构造函数的参数了
    $className = array_shift($arguments);
    //给所有的参数键值加个前缀
    $keys = array_keys($arguments);
    array_walk($keys, create_function(&#39;&$value, $key, $prefix&#39;, &#39;$value = $prefix . $value;&#39;), &#39;$arg_&#39;);
    //动态构造实例化类的函数,主要是动态构造参数的个数
    $paramStr = implode(&#39;, &#39;,$keys);
    $newClass=create_function($paramStr, "return new {$className}({$paramStr});");
    //实例化对象并返回
    return call_user_func_array($newClass, $arguments);
}

好了,至于效果是什么,就麻烦各位看官自己动动手,运行一下看看,是不是自己期望的结果。

The above is the detailed content of Detailed explanation of dynamic instantiation objects in the PHP framework. 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