php 在另一个文件中扩展类的正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8088832/
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-26 03:56:42  来源:igfitidea点击:

What is the proper way to extend a class in another file?

phpclassrequireextends

提问by diesel

This is what I have in foo.php

这就是我在 foo.php 中的内容

class Foo
{
    public $foo = NULL;
    public $foo2 = NULL;

    public function setFoo ($foo, $foo2)
    {
       $this->foo = $foo;
       $this->foo2 = $foo2'
    }
}

This is what I have in foo3.php

这就是我在 foo3.php 中的内容

class Foo3 extends Foo
{
    public $foo3 = NULL;

    public function setFoo3 ($foo3)
    {
       $this->foo = $foo3;
    }
}  

This is how I require it in my third file run.php:

这是我在第三个文件 run.php 中需要它的方式:

require_once "foo.php";
require_once "foo3.php";
$foo = new Foo();
$foo->setFoo3("hello");

I get this error:

我收到此错误:

Fatal error: Call to undefined method Foo::setFoo3()

致命错误:调用未定义的方法 Foo::setFoo3()

I'm not sure if the problem is how I'm requiring them. Thanks.

我不确定问题是否在于我如何要求它们。谢谢。

回答by Mike Purcell

In your example, you are instantiating Foo, which is the parent and has no knowledge of the method setFoo3(). Try this:

在您的示例中,您正在实例化Foo,它是父级并且不知道该方法setFoo3()。尝试这个:

class Foo3 extends Foo
{
    ...
}

require_once "foo.php";
require_once "foo3.php";
$foo = new Foo3();
$foo->setFoo3("hello");

回答by Dan K.K.

At the first, in your foo.phpshouldn't mark your fields public, because you set those values inside setFoo($foo1, $foo2)method. Instead, you may have something like:

首先,在您的foo.php 中不应标记您的 fields public,因为您在setFoo($foo1, $foo2)方法中设置了这些值。相反,您可能会遇到以下情况:

<?php

class Foo
{
    private $foo1;
    private $foo2;

    public function setFoo($foo1, $foo2) {
        $this->foo1 = $foo1;
        $this->foo2 = $foo2;
    }
 }

Then you should add extendskeyword when declaring class Foo3, and another thing you need to include extending class file in the beginning of the file. In your case you may have something like the following in your foo3.phpfile:

然后你应该extends在声明 class 时添加关键字Foo3,另外你需要在文件的开头包含扩展类文件。在您的情况下,您的foo3.php文件中可能包含以下内容:

<?php

require_once "foo.php";

class Foo3 extends Foo
{
    public function setFoo3($foo3) {
        $this->setFoo($foo3, "some foo3 specific value"); // calling superclass method
    }
}

then you can create an instantiate of a Foo3class in your run.phplike so:

然后你可以Foo3在你的run.php 中创建一个类的实例化,如下所示:

<?php

require_once "foo3.php";

$foo3 = new Foo3();
$foo3->setFoo3("bar");

and my advice, you should read a little about OOP techniques ;)

我的建议是,您应该阅读一些有关 OOP 技术的信息;)

回答by Oliver Charlesworth

Of course that doesn't work. You've created a Fooobject, and then tried to call foo3on it. But Foodoesn't have a foo3method.

这当然行不通。您已经创建了一个Foo对象,然后尝试调用foo3它。但是Foo没有foo3办法。