Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么要键入parent::\u构造?_Php_Codeigniter_Oop - Fatal编程技术网

Php 为什么要键入parent::\u构造?

Php 为什么要键入parent::\u构造?,php,codeigniter,oop,Php,Codeigniter,Oop,每次我去声明一个扩展类 Class cookies extends Food{ public function __construct() { parent::__construct(); } } 为什么我要使用parent::\uu construct() 这是遗产吗?仅仅扩展类不会继承它的属性吗 如果这是子类构造函数的唯一内容,那么根本不需要定义子构造函数,它只需运行父类构造函数即可 如果确实使用构造函数创建子类,则该子构造函数将运行,而不是父类构造

每次我去声明一个扩展类

Class cookies extends Food{
    public function __construct()
    {
        parent::__construct();
    }
}
为什么我要使用
parent::\uu construct()


这是遗产吗?仅仅扩展类不会继承它的属性吗

如果这是子类构造函数的唯一内容,那么根本不需要定义子构造函数,它只需运行父类构造函数即可


如果确实使用构造函数创建子类,则该子构造函数将运行,而不是父类构造函数(即使它只是一个空体);因此,如果您还需要运行父类的构造函数,那么您需要通过使用
父::u construct()

显式调用它,如果这是子类构造函数的唯一内容,那么您根本不需要定义子构造函数,它只需运行父构造函数即可


如果确实使用构造函数创建子类,则该子构造函数将运行,而不是父类构造函数(即使它只是一个空体);因此,如果您还需要运行父类的构造函数,那么您需要使用
parent::u construct()
显式调用它,这非常简单。如果您计划孩子的类有一个
\uuu构造
,那么您需要决定,“我是否也希望继承我父母的
\uu构造
”,如果是,那么您需要使用
父母::\uu构造()

以这个例子为例:

class Actions {

    function __construct() {
        echo "I can do even more actions and.. ";
    }

    function jump() {
        return "hop hop";
    }

    function eat() {
        return "munch munch";
    }
}

class Humans extends Actions {

    function __construct() { //this will overwrite the parents construct
        echo "I am a human and ...";
    }


    function talk() {
        return "I can talk";
    }

}

class Dogs extends Actions {

    function __construct() { //this will NOT overwrite the parents construct
        parent::__construct();
        echo "I am a dog and ...";
    }


    function bark() {
        return "I can bark";
    }

}
如果我们运行
Human()
函数,我们将得到以下结果:

$human = new Humans();
var_dump($human->talk());

/*
RESULT:

I am a human and ...
string 'I can talk' (length=10)
*/
$dogs = new Dogs();
var_dump($dogs->bark());

/*
RESULT:
I can do even more actions and.. I am a dog and ...
string 'I can bark' (length=10)
*/
但是,如果我们运行
Dogs()
函数,我们将得到以下结果:

$human = new Humans();
var_dump($human->talk());

/*
RESULT:

I am a human and ...
string 'I can talk' (length=10)
*/
$dogs = new Dogs();
var_dump($dogs->bark());

/*
RESULT:
I can do even more actions and.. I am a dog and ...
string 'I can bark' (length=10)
*/

这真的很简单。如果您计划孩子的类有一个
\uuu构造
,那么您需要决定,“我是否也希望继承我父母的
\uu构造
”,如果是,那么您需要使用
父母::\uu构造()

以这个例子为例:

class Actions {

    function __construct() {
        echo "I can do even more actions and.. ";
    }

    function jump() {
        return "hop hop";
    }

    function eat() {
        return "munch munch";
    }
}

class Humans extends Actions {

    function __construct() { //this will overwrite the parents construct
        echo "I am a human and ...";
    }


    function talk() {
        return "I can talk";
    }

}

class Dogs extends Actions {

    function __construct() { //this will NOT overwrite the parents construct
        parent::__construct();
        echo "I am a dog and ...";
    }


    function bark() {
        return "I can bark";
    }

}
如果我们运行
Human()
函数,我们将得到以下结果:

$human = new Humans();
var_dump($human->talk());

/*
RESULT:

I am a human and ...
string 'I can talk' (length=10)
*/
$dogs = new Dogs();
var_dump($dogs->bark());

/*
RESULT:
I can do even more actions and.. I am a dog and ...
string 'I can bark' (length=10)
*/
但是,如果我们运行
Dogs()
函数,我们将得到以下结果:

$human = new Humans();
var_dump($human->talk());

/*
RESULT:

I am a human and ...
string 'I can talk' (length=10)
*/
$dogs = new Dogs();
var_dump($dogs->bark());

/*
RESULT:
I can do even more actions and.. I am a dog and ...
string 'I can bark' (length=10)
*/

请阅读以下内容:。基本上你只需要它,如果你的子类有一个
\u构造函数的话。我们怎么知道你为什么使用:
父类::\u构造函数()
?!它只调用父构造函数。您应该只在父构造函数执行您需要的操作时调用它。通常,您只需要在父构造函数的作用之外(或在准备父构造函数的作用时)执行此操作。请阅读以下内容:。基本上你只需要它,如果你的子类有一个
\u构造函数的话。我们怎么知道你为什么使用:
父类::\u构造函数()
?!它只调用父构造函数。您应该只在父构造函数执行您需要的操作时调用它。通常情况下,您只需要在父构造函数的作用之外(或在准备父构造函数的作用时)执行此操作。因此,如果子类除了父类之外还有其他要求,我会在子类的构造函数中使用parent::u construct(),然后包括任何所需的附加内容?完全正确,否则,通常最好不要在子类中定义构造函数,而只是调用父类构造函数。。。。请记住,如果父构造函数需要参数,并且您也有子构造函数,那么它需要自己接受这些参数,并在调用父构造函数时传递这些参数。如果子类除了父类之外还有其他要求,我将使用parent::\uu construct()在子的构造中,然后包含任何必需的附加项?完全正确,否则通常最好不要在子的构造中定义构造函数,而只是调用父构造函数。。。。请记住,如果父构造函数需要参数,并且您也有子构造函数,那么它需要自己接受这些参数,并在调用父构造函数时传递这些参数