开发者

PHP OOP problem

I have classes Video开发者_开发问答 and Audio, which extend class Popular

In class, Video & Audio is a function of "public static function parsing ()" are different in themselves

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"

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. Then your getSong just looks like this.

EDIT: self keyword is bound to the class in which the function is defined. 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.

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

below is wrong


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

But really, getSong is no longer needed. 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.

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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜