简体   繁体   English

PHP:父类和子类

[英]PHP: Parent Class and a Child Class

I am trying to learn more about OOP in PHP so I setup a simple situation. 我试图学习有关PHP中OOP的更多信息,所以我设置了一个简单的情况。

I have a MySQL database that holds a table with wallets. 我有一个MySQL数据库,其中包含一个带有钱包的表。 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: 要了解有关OOP的更多信息,我想构建它:

$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. 所以我想我需要在WalletConnection类中添加一个getWallet()函数,然后从那里调用“ new Wallet”。

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. 目前,我不知道我是否以正确的方式进行操作,如果我是正确的人,则需要知道下一步该怎么做以确保例如getBalance()函数可以使用父级的$ db连接。

If you want to learn more about OOP start by learning the principals of it. 如果要了解有关OOP的更多信息,请先学习OOP的原理。 http://codebetter.com/raymondlewallen/2005/07/19/4-major-principles-of-object-oriented-programming/ 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. 您应该特别深入研究Encapsulation主体,因为您在数据库和电子钱包之间建立了依赖关系,从而在这里违反了它。

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). 更好的做法是将一个钱包作为模型,并创建一个负责与其相关功能的服务(例如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. 创建一个PDO适配器。
  2. Use the PDO Adapter to get the raw SQL wallet data 使用PDO适配器获取原始SQL钱包数据
  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: 而且您应该可以通过公开或通过getter来使用其属性:

$balance = $wallet->balace;

or 要么

$balance = $wallat->getBalance();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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