简体   繁体   中英

How to call method with the same name from parent class in instance scope

I have parent class c1 with function hi(). class c2 extends c1 and contains singleton access through method i(). In class c2 I want to make method with the same name as in c1, which I can call in static scope, but in this method I whan to use singleton instance and call method with the same name from parent class.

class c1{
  function hi(){
    echo 'very '.$this->a;
  }
}

class c2 extends c1{
    private static $i;
    public static function i()  {
        return (self::$i)?self::$i:(self::$i = new self);
    }    

    public $a='cool';
  function hi(){
    self::i()->hi(); // here I want to call hi() from c1 but in instance scope
  }
}

c2::hi(); //must echo: very cool

In real example
c1 is mysqli
c2 is my custom db class
and hi() is commit()
I want to be able to call db::commit(), so i dont need to write db::i()->commit();



EDIT: PHP < 5.3

Normally the way to invoke the class's parent's method is to use the parent:: keyword, but this only works when you run in instance scope, not in static scope.

But you can't run hi() as both a static function and a non-static function.

class c1
{
  function hi() {
    echo 'very '.$this->a;
  }
}

class c2 extends c1
{
  private static $i;

  public static function i()  {
        return (self::$i)? self::$i: (self::$i = new c2());
  }

  public $a='cool'; // not static

  function hi() {
    parent::hi();
  }

  static function do_hi() {
    $i = self::i();
    $i->hi();
  }
}

c2::do_hi(); // echoes: very cool

Additional comment: Strangely, PHP allows you to call instance methods as if they were static methods, using the :: syntax. In my opinion, this is wrong and it leads to confusing code. You should pretend that this usage is not possible, and instead call function statically only if you declare them static .

I don't think that trying to be clever and force code to do double-duty is a good practice. Also you should not be too fond of one-line functions that are complex and hard to read or modify. It's common for programmers to favor overly compact code, but you'll find that it makes the code hard to maintain. Project requirements inevitably change, and you'll end up having to pull apart your one-line gems anyway.

您可以静态调用mysqli_commit

parent::commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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