简体   繁体   English

PHP OOP问题

[英]PHP OOP problem

I have classes Video and Audio , which extend class Popular 我有VideoAudio课程,这扩展了Popular课程

In class, Video & Audio is a function of "public static function parsing ()" are different in themselves 在课堂上,Video&Audio是一个函数,“ public static function parsing()”本身是不同的

So, as I can from the class of "Popular" address to the class that calls? 那么,从“热门”地址类到调用该类的我可以吗? That is in a class of "Popular" is a function of "getSong" which has a function call "parsing ()", but here's how to call this function from the class that turns .. Hopefully clearly explained .. Kind of like the opposite function of need "parent" 那是在“ Popular”类中的一个“ getSong”函数,它具有一个函数调用“ parsing()”,但是这是如何从该类中调用该函数的。希望可以清楚地解释..有点像需要“父母”的相反功能

class Video extends Popular {

    public static function parsing()
    {
        return 'Videooooooooooooo';
        .....
    }

    public static fuinction tratata()
    {
    ....
    }

}

class Audio extends Popular {

    public static function parsing()
    {
       .............
        return 'Audio';
        .....
    }

    public static fuinction tratata()
    {
    ....
    }

}

class Popular {

    public static function getSong()
    {
        $class = '???????';//Video or Audio???
        $class::parsing();// ???
    }

}

Sorry for bad english.. used google traslate 抱歉英语不好..使用谷歌翻译

There is no need for this behavior. 不需要这种行为。

Put the parsing function in the Popular class, so that Audio and Video inherit it. parsing函数放在Popular类中,以便AudioVideo继承它。 Then your getSong just looks like this. 然后,您的getSong看起来就像这样。

EDIT: self keyword is bound to the class in which the function is defined. 编辑: self关键字绑定到在其中定义函数的类。 So the below is wrong. 所以下面是错误的。 The correct answer is to use the static keyword instead - IF you are using PHP 5.3.0 or later. 正确的答案是改为使用static关键字-如果您使用的是PHP 5.3.0或更高版本。

public static function getSong()
{
    static::parsing();
}

below is wrong 下面是错的


public static function getSong()
{
    self::parsing();
}

But really, getSong is no longer needed. 但实际上,不再需要getSong。 When a base class implementation is called on a child class, self still refers to the child class, so the correct parsing function will be called automatically. 当在子类上调用基类实现时, self仍然引用该子类,因此将自动调用正确的parsing函数。

Did I understand your question correctly? 我是否正确理解您的问题?

Also to everyone else: if I'm wrong about this behavior, then shame on me and I need to go back to school. 对其他所有人也是如此:如果我对这种行为不对,那就让我感到羞耻,我需要回到学校。 I haven't used this behavior in a while so I may be rusty. 我有一段时间没有使用这种行为了,所以我可能会生锈。

@Tesserex probaly has the best answer. @Tesserex可能是最好的答案。

Not sure whether this is what you mean, but to find out the class name of the current object: 不确定这是否是您的意思,但是要找出当前对象的类名称:

You seem to be using these functions in static context. 您似乎在静态上下文中使用这些功能。 That makes things more difficult. 这使事情变得更加困难。 As far as I know, the only way to find out in that case is PHP 5.3's get_called_class() . 据我所知,在这种情况下找出的唯一方法是PHP 5.3的get_called_class()

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

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