Search code examples
phpconstructorconstruct

call the construct class again trough another function of the class in PHP?


I want to call the __construct function again in my class

something like this:

class user{
    function __construct($ID = null)
    {
        if($ID){
            //code
        }

    function findUser()
    {
        //code
        $this->__construct($ID);
    }
}

of course this does not work, but what is the correct way to do this?


Solution

  • If you want to overwrite the current values in the current instance, I would do this:

    class user{
        function __construct($ID = null)
        {
            $this->reinit($ID);
        }
    
        function reinit($id)
        {
            if($id) {
                //code
            }
        }
    }