开发者

php polymorphism - Call a parent function in polymorphed child function

开发者 https://www.devze.com 2023-02-12 07:53 出处:网络
Here\'s the parent class: class Event { public function getLang($lang){ $sql = \"select * from Event where EventID =\" . $this->EventID . \"AND Lang =\" . $lang;

Here's the parent class:

    class Event {

    public function getLang($lang){
     $sql = "select * from Event where EventID =" . $this->EventID . "AND Lang =" . $lang;
    $result = $this->selectOneRow($sql);
    }
}

and here's the child:

class Invitation extends Event{

public function getLang($lang){
Event::getLang($lang);

$sql = "select * from invitation where EventID =" . $this->EventID . " and Lang = " . $lang;

    }
}

I had some hope that EVENT::getLang($lang) would work but after I echo the query, I can see that it stops short of an EventID.

Is there a right way to do this?

I tried copy/pasting the code in the child directly but that can't work either because, I got variables at the parent's level to which the result of event's select will be assigned.

Is there any way to work around this or am I in a g开发者_StackOverflow社区ridlock?


I think you're looking to use the parent keyword:

class Invitation extends Event{
    public function getLang($lang){
       parent::getLang($lang);

       $sql = "SELECT * FROM invitation WHERE EventID =" . $this->EventID . " AND Lang = " . $lang;
    }
}


You have to use parent

class Invitation extends Event{

    public function getLang($lang){
        parent::getLang($lang);
        ....
    }
}

Event::getLang($lang); is infact trying to call getLang statically. See these links:

Scope Resolution Operator (::)

Parent


The method is not static, so instead of calling the static method Event::getLang(), you need to call parents method

parent::getlang($lang);

Update: I meant, with Event::getLang() you usually call a static method on a class, that may or may not be extended. Where parent::method() calls always the inherited method and keeps the scope (class or static) of the calling method, Classname::method() always tries to call a static method on the specific class.

0

精彩评论

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