PHP:在构造函数中调用用户定义的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4335618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:43:17  来源:igfitidea点击:

PHP: Calling a user-defined function inside constructor?

phpfunctionmethodsconstructor

提问by Prashant

I have a class userAuthinside 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 ConfirmUserto 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.

userAuth在它的构造函数中有一个类我添加了代码来检查用户是否有效,如果会话中没有值,那么我检查 cookie(作为“记住我”功能的一部分),如果 cookie 中有一些值然后我调用一个函数ConfirmUser从数据库中检查它的真实性。根据confirmUser 函数返回的值,我在构造函数中返回一个bool(true 或fales)值。

I have created my class as:

我已经创建了我的班级:

<?php
    class userAuth {

        function userAuth(){
            //code
        }

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

    $signin_user = new userAuth();

?>

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

confirmUser函数接受两个字符串类型参数并返回一个整数值 0、1、2。

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

我无法confirmUser在构造函数中添加函数代码,因为我在应用程序的更多地方使用了这个函数。

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

所以,我想知道如何在 PHP 的构造函数中调用用户定义的函数。请帮忙。

Thanks!

谢谢!

回答by DampeS8N

$this->nameOfFunction()

$this->nameOfFunction()

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

但是当它们在一个类中时,它们被称为方法。

回答by adsc

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

但是,在构造函数中使用 $this 时要小心,因为在扩展层次结构中,它可能会导致意外行为:

<?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

回答by Caner

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()

在构造函数内部调用函数和从其他地方调用没有区别。如果该方法在同一个类中声明,则应使用$this->function()

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

顺便说一句,在 php5 中,建议您像这样命名构造函数:
function __construct()

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

如果没有,则public在构造函数定义之前放置关键字,如下所示public function userAuth()

回答by Shakti Singh

you can call with $this

你可以用 $this 打电话

<?php
    class userAuth {

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

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

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

?>