11

I have a class userAuth inside its constructor I have added code to check the user is valid or not, if there is no value in session then I check cookies (as a part of "Remember Me" feature), if there is some value inside cookies then I call a function ConfirmUser to check its authenticity from database. On the basis of value returned by the confirmUser function I am returning a bool (true or fales) value in constructor.

I have created my class as:

<?php
    class userAuth {

        function userAuth(){
            //code
        }

        function confirmUser($username, $password){
                   //code
        }
    }

    $signin_user = new userAuth();

?>

confirmUser function take two string type parameters and return return a integer value of 0, 1, 2.

I can't add code of confirmUser function inside the constructor as I am using this function at some more places in my application.

So, I want to know how to call a user-defined function inside constructor in PHP. Please help.

Thanks!

3
  • Put some code in question. There is no difference where to call function. Commented Dec 2, 2010 at 13:46
  • 1
    Ensure include/require for function declaration of confirmUser, and you can call confirmUser inside constructor...what is the problem? Commented Dec 2, 2010 at 13:47
  • You said you're returning a bool in the constructor which is a bit strange. A constructor cannot return anything but an instance of its class. Can you elaborate? Commented Dec 2, 2010 at 14:20

4 Answers 4

30

$this->nameOfFunction()

But when they are in a class, they are called Methods.

Sign up to request clarification or add additional context in comments.

1 Comment

I suspect you thought that until the constructor was complete, there wasn't a '$this' to call methods on; but there is. In PHP the new object exists, and '$this' points to it, as soon as it enters the constructor.
9

Be careful with using $this in a constructor, though, because in an extension hierarchy, it can cause unexpected behaviour:

<?php

class ParentClass {
    public function __construct() {
        $this->action();
    }

    public function action() {
        echo 'parent action' . PHP_EOL;
    }
}

class ChildClass extends ParentClass {
    public function __construct() {
        parent::__construct();
        $this->action();
    }

    public function action() {
        echo 'child action' . PHP_EOL;
    }
}

$child = new ChildClass();

Output:

child action
child action

Whereas:

class ParentClass {
    public function __construct() {
        self::action();
    }

    public function action() {
        echo 'parent action' . PHP_EOL;
    }
}

class ChildClass extends ParentClass {
    public function __construct() {
        parent::__construct();
        self::action();
    }

    public function action() {
        echo 'child action' . PHP_EOL;
    }
}

$child = new ChildClass();

Output:

parent action
child action

1 Comment

Thanks for explaining this very important difference between this and self::
3

There is no difference between calling a function inside a constructor and calling from somewhere else. If the method is declared in the same class you should use $this->function()

By the way, in php5 you are suggested to name your constructor like this:
function __construct()

If not then put public keyword before your constructor definition like this public function userAuth()

Comments

2

you can call with $this

<?php
    class userAuth {

        function userAuth($username, $password){
             $this->confirmUser($username, $password);
        }

        function confirmUser($username, $password){
                   //code
        }
    }

    $signin_user = new userAuth($username, $password);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.