简体   繁体   English

如果父类中不存在子类,则调用子类函数

[英]Calling a child class function if not exist in parent class

I have a code like following --- 我有一个类似下面的代码-

class CartItem{
    var $v;
    function __construct(){
        $this->f();
    }
    function f(){
         echo 'In parent';
    }
}

class m extends CartItem{
    function f(){
        echo 'In child';
    }
}

new m();

Now when creating instance of m()... it doesn't have any constructor, so it is calling parent classes constructor. 现在,当创建m()的实例时...它没有任何构造函数,因此它正在调用父类的构造函数。 Now inside that a function f is called. 现在在其中调用了函数f。

What I want is - if class m() have defined function f()... is should call it instead of parent class's function f(). 我想要的是-如果类m()具有定义的函数f()...,则应调用它而不是父类的函数f()。

But anyway it is calling parent classes function, as it was called from parent's constructor, irrespective of child class/ context :( 但是无论如何,它都在调用父类函数,就像从父类的构造函数中调用的那样,而与子类/上下文无关:(

You want to call in __construct() a method that is not defined in the class. 您想在__construct()调用该类中未定义的方法。 This is a sign that the CartItem class is an abstract concept and you don't intend to instantiate it (because an instance of CartItem probably doesn't contain enough information or behaviour for your project). 这表明CartItem类是一个抽象概念,您不打算实例化它(因为CartItem的实例可能不包含项目的足够信息或行为)。

An abstract concept is implemented using an abstract class that defines as much as it can and defines abstract methods to be implemented in the concrete classes that extend it. 使用一个抽象类来实现一个抽象概念 ,该抽象类定义了尽可能多的抽象类 ,并定义了在扩展它的具体类中实现的抽象方法。 The method f() is such a method that cannot be defined in the abstract concept and has to be defined in each class that extend it: 方法f()是无法在抽象概念中定义的方法,而必须在扩展它的每个类中定义:

abstract class CartItem
{
    public function __construct()
    {
        $this->f();
    }

    abstract protected function f();
}

class m extends CartItem
{
    protected function f()
    {
        // Implement behaviour specific to this class
    }
}

This is actually a really interesting question. 这实际上是一个非常有趣的问题。

so, as I understand it, you're asking ( if this isnt right please say ): 因此,据我所知,您是在问( 如果这样不对,请说 ):

can you call a function of a class that's extending a parent? 您可以调用扩展父类的类的函数吗?

yes, you can... sort of, if the method in the child is static. 是的,如果孩子中的方法是静态的,则可以...

Take this example (Ive not used it in the constructor for simplicity of example, but it will work there too): 以这个例子为例(为了简化示例,我没有在构造函数中使用它,但是它也可以在其中使用):

class ClassA {

    public function testMeAsWell() {
        return ClassB::testMe();
    }
}

class ClassB extends ClassA {

    static function testMe() {
        return 'do something';
    }
}

$child = new ClassB();
echo $child->testMe();

// outputs 'do something'


$parent = new ClassA();
echo $parent->testMeAsWell();

// also outputs 'do something'

the reason this works needs more research, but as a guess I would say that because PHP is compiled, it will know about both classes at run-time and therefore will be able to figure out what we wanted it to do. 这项工作的原因需要更多的研究,但是我猜想是因为PHP是编译的,因此它将在运行时了解这两个类,因此可以弄清楚我们希望它做什么。

So, further on, you want to use variables. 因此,进一步,您想使用变量。 Yes you can, but they would have to be static as well. 是的,您可以,但是它们也必须是静态的。

working example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM