开发者

extending a superclass' constructor within a subclass in php [duplicate]

This question already has answers here: 开发者_如何学Go Closed 12 years ago.

Possible Duplicate:

Call parent constructor before child constructor in PHP

I have a class that manages my database connection. It has a constructor that fetches the database details from a config file:

    class Database {
        function __construct(){
        //perform magic
        }
    }

I am now extending this class to create a class for managing user creation and validation, and I need it to still do the things the superclass does, but with some extras.

class Members {
      function __construct(){
       //perform super class magic
       // then perform your own magic
      }
}

What is the correct way to go about this?


Use parent::__construct:

class Members {
    public function __construct(){
        parent::__construct();

        // your code
    }
}

Obviously you'll need to pass any arguments on that the parent class requires.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜