PHP:现实世界中的OOP示例 [英] PHP: Real world OOP example

查看:54
本文介绍了PHP:现实世界中的OOP示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习OOP.我正在阅读的书中所谓的现实世界"示例无济于事.

I am trying to learn OOP. The so called 'real world' examples in the books I am reading aren't helping.

PetCarHuman之类的所有示例都不再对我有帮助.我需要真实的生活示例,例如注册,用户个人资料页面等.

All the examples like Pet, Car, Human aren't helping me anymore. I need REAL LIFE examples that like registration, user profile pages, etc.

一个例子:

$user->userName = $_POST['userName'];//save username
$user->password = $_POST['password'];//save password
$user->saveUser();//insert in database

我还看过:

$user->user = (array) $_POST;

其中:

private $user = array();

将所有信息保存在一个数组中.

Holds all the information in an array.

并且在同一个班级之内

$user->getUser($uid);
// which sets the $this->user array equal to mysqli_fetch_assoc() using 
//the user id.

在许多不同的php应用程序(注册,登录名,用户帐户等)中是否存在实现OOP的现实示例?

Are there any real world examples implementing OOP in the many different php applications (registration, login, user account, etc)?

推荐答案

OOP不仅与单个类的外观和操作有关.它还涉及一个或多个类的实例如何协同工作.

OOP is not only about how a single class looks and operates. It's also about how instances of one or more classes work together.

这就是为什么您看到这么多基于汽车"和人"的示例的原因,因为它们实际上在说明这一原理方面做得非常好.

That's why you see so many examples based on "Cars" and "People" because they actually do a really good job of illustrating this principle.

我认为,OOP中最重要的课程是封装多态性.

In my opinion, the most important lessons in OOP are encapsulation and polymorphism.

封装:以简明合理的方式将数据和对数据进行操作的逻辑耦合在一起 多态性::一个对象看起来像和/或行为类似的能力.

Encapsulation: Coupling data and the logic which acts on that data together in a concise, logical manner Polymorphism: The ability for one object to look-like and/or behave-like another.

一个很好的现实例子是目录迭代器.该目录在哪里?也许是本地文件夹,或者像FTP服务器一样远程.谁知道!

A good real-world example of this would be something like a directory iterator. Where is this directory? Maybe it's a local folder, maybe it's remote like an FTP server. Who knows!

这是演示封装的基本类树:

Here's a basic class tree that demonstrates encapsulation:

<?php

interface DirectoryIteratorInterface
{
    /**
     * @return \Traversable|array
     */
    public function listDirs();
}

abstract class AbstractDirectoryIterator implements DirectoryIteratorInterface
{
    protected $root;

    public function __construct($root)
    {
        $this->root = $root;
    }
}

class LocalDirectoryIterator extends AbstractDirectoryIterator
{
    public function listDirs()
    {
        // logic to get the current directory nodes and return them
    }
}

class FtpDirectoryIterator extends AbstractDirectoryIterator
{
    public function listDirs()
    {
        // logic to get the current directory nodes and return them
    }
}

每个类/对象都负责其自己的检索目录列表的方法.数据(变量)耦合到使用该数据的逻辑(类函数,即方法).

Each class/object is responsible for its own method of retrieving a directory listing. The data (variables) are coupled to the logic (class functions i.e, methods) that use the data.

但是故事还没有结束-还记得我所说的OOP是关于类实例如何协同工作的,而不是单个类或对象如何协同工作的吗?

But the story is not over - remember how I said OOP is about how instances of classes work together, and not any single class or object?

好吧,让我们对这些数据做些什么-将其打印到屏幕上?当然.但是如何? HTML?纯文本? RSS?让我们解决这个问题.

Ok, so let's do something with this data - print it to the screen? Sure. But how? HTML? Plain-text? RSS? Let's address that.

<?php

interface DirectoryRendererInterface
{
    public function render();
}

abstract class AbstractDirectoryRenderer implements DirectoryRendererInterface
{
    protected $iterator;

    public function __construct(DirectoryIteratorInterface $iterator)
    {
        $this->iterator = $iterator;
    }

    public function render()
    {
        $dirs = $this->iterator->listDirs();
        foreach ($dirs as $dir) {
            $this->renderDirectory($dir);
        }
    }

    abstract protected function renderDirectory($directory);
}

class PlainTextDirectoryRenderer extends AbstractDirectoryRenderer
{
    protected function renderDirectory($directory)
    {
        echo $directory, "\n";
    }
}

class HtmlDirectoryRenderer extends AbstractDirectoryRenderer
{
    protected function renderDirectory($directory)
    {
        echo $directory, "<br>";
    }
}

好吧,现在我们有几个类树用于遍历和呈现目录列表.我们如何使用它们?

Ok, now we have a couple class trees for traversing and rendering directory lists. How do we use them?

// Print a remote directory as HTML
$data = new HtmlDirectoryRenderer(
  new FtpDirectoryIterator('ftp://example.com/path')
);
$data->render();

// Print a local directory a plain text
$data = new PlainTextDirectoryRenderer(
  new LocalDirectoryIterator('/home/pbailey')
);
$data->render();

现在,我知道您在想什么,但是,彼得,我不需要这些大型树来执行此操作!"但是,如果您认为那样就错了,就像我怀疑您曾经接触过汽车"和人"的例子一样.不要专注于示例的细节-而是尝试了解这里发生的事情.

Now, I know what you're thinking, "But Peter, I don't need these big class trees to do this!" but if you think that then you're missing the point, much like I suspect you have been with the "Car" and "People" examples. Don't focus on the minutiae of the example - instead try to understand what's happening here.

我们创建了两个类树,其中一个(*DirectoryRenderer)以一种预期的方式使用另一个(*DirectoryIterator)-这通常称为 contract . *DirectoryRenderer实例不在乎它接收的*DirectoryIterator实例类型,也不在乎*DirectoryIterator实例如何呈现它们.

We've created two class trees where one (*DirectoryRenderer) uses the other (*DirectoryIterator) in an expected way - this is often referred to as a contract. An instance of *DirectoryRenderer doesn't care which type of instance of *DirectoryIterator it receives, nor do instances of *DirectoryIterator care about how they're being rendered.

为什么?因为我们是按照这种方式设计的.他们只是彼此插入并工作. 这是行动中的OOP .

Why? Because we've designed them that way. They just plug into each other and work. This is OOP in action.

这篇关于PHP:现实世界中的OOP示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆