简体   繁体   中英

PHP: Parent Class and a Child Class

I am trying to learn more about OOP in PHP so I setup a simple situation.

I have a MySQL database that holds a table with wallets. Then I made the following class:

class WalletConnection {

    private $db;
    private $user_id;
    private $wallets;

    public function __construct ($user_id) {

        $this->user_id = $user_id;
        $this->db = new PDO('mysql:host=localhost;dbname=ws;charset=utf8', 'dbuser', '***');
        $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    }

    public function loadWallets () {

        $sql = $this->db->prepare('SELECT id, CurrencyCode, Balance FROM wallets WHERE UserID = :UserID');
        $sql->execute([':UserID' => $this->user_id]);
        $this->wallets = $sql->fetchAll(PDO::FETCH_ASSOC);

    }

    public function getWallet ($num) {

        // do something like: new Wallet($this->wallets[$num])

    }

}

Then I create the wallet connection like this:

$wallets = new WalletConnection(40); // 40 = UserID of wallet owner
$wallets->loadWallets();

Now I want to create a child class Wallet to work with individual wallets.

class Wallet extends WalletConnection {

    private $id, $balance, $currency_code;

    public function __construct($data) {
        $this->id = $data['id'];
        $this->balance = $data['Balance'];
        $this->currency_code = $data['CurrencyCode'];
    }

    public function getBalance() {
    }

}

To learn more about OOP I want to build this:

$wallet = $wallets->getWallet(0); // This will now contain the id, CurrencyCode, Balance of the first wallet of the parent's $wallets.

So I think I need to add a getWallet() function to the WalletConnection class and call "new Wallet" from there.

And then in the client code I want to do:

$wallet->getBalance()

At the moment I don't know if I am doing this the right way, and if I am I need to know what to do next to make sure that for example the getBalance() function can use the parent's $db connection.

If you want to learn more about OOP start by learning the principals of it. http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/

You should dive deep into the Encapsulation principal specifically since you have violated it here by making a dependency between your DB and your Wallet.

A better practice would be to make a wallet as model, and a service which is in charge of the functionality related to it (such as getWalletById).

You would want a mapper that knows how to take the information from the database (usually array) and maps it to the wallet model.

Also you want to use an adapter that can connect to the database and NOT just work with wallets.

Remember that extending is not always the solution, sometimes you should use use composition over inheritance.

For example, the service contains a mapper, and the mapper contains the adapter.

To sum things up, try something like this:

  1. Create a wallet model. Create a wallet service. Create a wallet mapper. Create a PDO adapter.
  2. Use the PDO Adapter to get the raw SQL wallet data
  3. Map the array to the proper wallet model and return it through the mapper
  4. Return the Wallet model through the service.

In the end you should have a single line of code:

$wallet = $walletService->getWalletById($id);

And you should be able to use its attributes by being public or by a getter:

$balance = $wallet->balace;

or

$balance = $wallat->getBalance();

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