Home > Backend Development > PHP Tutorial > Solution to the problem of multiple constructors in php

Solution to the problem of multiple constructors in php

黄舟
Release: 2023-03-12 10:14:02
Original
2774 people have browsed it

To implement a constructor is two variables , and the other constructor is an array .
For example:

public function construct($id, $dname)
public function construct($device=array())
Copy after login

I only found on the Internet that it can be implemented by judging the number of parameters through func_num_args. Is there any way to implement it by judging the type?

It is impossible to have two method functions with the same name in one file at the same time
2. If they are in different files, you can use namespace to implement the same function Call (php version must be 5.3.0 or above)
So your judgment has no real meaning

PHP cannot be overloaded, change the parameters to an array and make the difference within the function

I Want to see how they coexist in the source code, inheritance?

gettype -- Get the type of the variable

construct is not the constructor of the php class
construct is!

Although php does not support overloading, the poster did not say that it is a method with the same name in a class

Judge the parameter type in the constructor construct, and then take different branches That's it. If you don’t support overloading natively, just simulate it~

php does not support function overloading like C. Just like you said, use the func_num_arg function and then judge the parameters to branch initialization

You can determine the number and type of parameters in construct.

<?php
 
class demo{
 
    private $_args;
 
    public function construct(){
        $args_num = func_num_args();
        if($args_num==2){
            $this->_args = array(
                                &#39;id&#39; => func_get_arg(0),
                                &#39;dname&#39; => func_get_arg(1)
                            );
        }elseif($args_num==1 && is_array(func_get_arg(0))){
            $this->_args = array(
                                &#39;device&#39;=>func_get_arg(0)
                            );
        }else{
            exit(&#39;func param not match&#39;);
        }    
    }
 
    public function show(){
        echo &#39;<pre class="brush:php;toolbar:false">&#39;;
        print_r($this->_args);
        echo &#39;
'; } } // demo1 $id = 1; $dname = 'fdipzone'; $obj = new demo($id, $dname); $obj->show(); // demo2 $device = array('iOS','Android'); $obj = new demo($device); $obj->show(); ?>
Copy after login

The above is the detailed content of Solution to the problem of multiple constructors in php. 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