Home > Backend Development > PHP Tutorial > PHP uses multiple constructed methods

PHP uses multiple constructed methods

小云云
Release: 2023-03-21 20:16:01
Original
1443 people have browsed it

The reason why PHP cannot define multiple constructors is: because PHP is a weakly typed language, the input parameter type of the function cannot be determined (type hints can be used, but type hints cannot be used for integers, strings, etc. scalar type), and for a function, for example, only 3 input parameters are defined, but PHP inputs 4 or more parameters when calling. Therefore, based on these two points, it is destined that functions cannot be overloaded in PHP (similar to the Javascript language), and there cannot be overloading of constructors.

PHP’s construction method is different from Java’s. There cannot be multiple construction methods. There can only be one construction method. If you want to use the construction method similar to Java, you can use the following method to achieve it. :

[php] view plain copy

  1. //PHP构造方法使用
    class Test{  
        function __construct(){  
            echo &#39;Test类无参构造方法被调用<br/>&#39;;  
            $a=func_get_args();  
            $i=func_num_args();  
            //判断Test类是否有__constructxx方法,将方法名记为$f
            if(method_exists($this,$f=&#39;__construct&#39;.$i)){  
                //若存在xx方法,使用call_user_func_array(arr1,arr2)函数调用他,该函数的参数为两个数组,前面的数组为调用谁($this)的什么($f)方法,后一个数组为参数
                call_user_func_array(array($this,$f),$a);  
            }  
        }  
        function __construct1($a1){  
            echo &#39;Test类1个参数的仿构造方法被调用<br/>&#39;;  
            //输出参数值
            echo &#39;其值为:&#39;.$a1.&#39;<br/><br/>&#39;;  
        }  
        function __construct2($a1,$a2){  
            echo &#39;Test类2个参数的仿构造方法被调用<br/>&#39;;  
            echo &#39;其值为:&#39;.$a1.&#39;、&#39;.$a2.&#39;<br/><br/>&#39;;  
        }  
        function __construct3($a1,$a2,$a3){  
            echo &#39;Test类3个参数的仿构造方法被调用<br/>&#39;;  
            echo &#39;其值为:&#39;.$a1.&#39;、&#39;.$a2.&#39;、&#39;.$a3.&#39;<br/><br/>&#39;;  
        }  
    }  
    //测试
    new Test(&#39;你&#39;);  
    new Test(&#39;你&#39;,&#39;好&#39;);  
    new Test(&#39;你&#39;,&#39;好&#39;,&#39;啊&#39;);
    Copy after login

##The test result is:


Related recommendations:

Detailed explanation of PHP language constructor

Detailed explanation of js ordinary functions and constructors

JavaScript Constructor Pattern Example Analysis

The above is the detailed content of PHP uses multiple constructed methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template