PHP:现实世界的OOP示例(PHP: Real world OOP example)

编程入门 行业动态 更新时间:2024-10-23 19:33:37
PHP:现实世界的OOP示例(PHP: Real world OOP example)

我正在尝试学习OOP。 我正在阅读的书中所谓的“真实世界”的例子没有帮助。

所有像Pet , Car , Human的例子都没有帮助我了。 我需要REAL LIFE实例,例如注册,用户个人资料页面等。

一个例子:

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

我也看过:

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

其中:

private $user = array();

将所有信息保存在数组中。

在同一个课堂里呢

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

在许多不同的PHP应用程序(注册,登录,用户帐户等)中是否有实现OOP的真实世界示例?

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

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

An example:

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

I've also seen:

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

where :

private $user = array();

Holds all the information in an array.

And within that same class lies

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

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

最满意答案

OOP不仅仅是一个类的外观和操作。 这也是关于一个或多个类的实例如何协同工作。

这就是为什么你看到如此多的基于“汽车”和“人”的例子,因为他们真的很好地说明了这个原则。

在我看来,OOP中最重要的教训是封装多态性

封装:以简明,逻辑的方式将数据和逻辑结合在一起数据多态:一个对象看起来和/或表现得像另一个对象的能力。

一个很好的现实世界的例子就像一个目录迭代器。 这个目录在哪里? 也许这是一个本地的文件夹,也许它像远程FTP服务器。 谁知道!

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

<?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 } }

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

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

好的,让我们用这些数据做一些事情 - 把它打印到屏幕上? 当然。 但是怎么样 HTML? 纯文本? RSS? 我们来解决一下。

<?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>"; } }

好的,现在我们有一些类树来遍历和渲染目录列表。 我们如何使用它们?

// 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();

现在,我知道你在想什么,“但是彼得,我不需要这些大类树来做这个!” 但是如果你认为那么你就错过了这一点,就像我怀疑你曾经是“汽车”和“人”的例子。 不要专注于这个例子的细节 - 而是试图了解这里发生了什么。

我们创建了两个类树,其中一个( *DirectoryRenderer )以预期的方式使用另一个( *DirectoryIterator ) - 这通常被称为合同 。 *DirectoryRenderer一个实例不关心它接收到哪个类型的*DirectoryIterator类型,而且*DirectoryIterator实例也不关心它们的呈现方式。

为什么? 因为我们这样设计了它们。 他们只是互相插手并且工作。 这是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.

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.

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.

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?

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.

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.

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

更多推荐

本文发布于:2023-07-30 22:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   现实   世界   PHP   world

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!