Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>In my opinion, the most important lessons in OOP are <strong>encapsulation</strong> and <strong>polymorphism</strong>.</p> <p><strong>Encapsulation:</strong> Coupling data and the logic which acts on that data together in a concise, logical manner <strong>Polymorphism:</strong> The ability for one object to look-like and/or behave-like another.</p> <p>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!</p> <p>Here's a basic class tree that demonstrates encapsulation:</p> <pre><code>&lt;?php interface DirectoryIteratorInterface { /** * @return \Traversable|array */ public function listDirs(); } abstract class AbstractDirectoryIterator implements DirectoryIteratorInterface { protected $root; public function __construct($root) { $this-&gt;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 } } </code></pre> <p>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.</p> <p>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?</p> <p>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.</p> <pre><code>&lt;?php interface DirectoryRendererInterface { public function render(); } abstract class AbstractDirectoryRenderer implements DirectoryRendererInterface { protected $iterator; public function __construct(DirectoryIteratorInterface $iterator) { $this-&gt;iterator = $iterator; } public function render() { $dirs = $this-&gt;iterator-&gt;listDirs(); foreach ($dirs as $dir) { $this-&gt;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, "&lt;br&gt;"; } } </code></pre> <p>Ok, now we have a couple class trees for traversing and rendering directory lists. How do we use them?</p> <pre><code>// Print a remote directory as HTML $data = new HtmlDirectoryRenderer( new FtpDirectoryIterator('ftp://example.com/path') ); $data-&gt;render(); // Print a local directory a plain text $data = new PlainTextDirectoryRenderer( new LocalDirectoryIterator('/home/pbailey') ); $data-&gt;render(); </code></pre> <p>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.</p> <p>We've created two class trees where one (<code>*DirectoryRenderer</code>) uses the other (<code>*DirectoryIterator</code>) in an expected way - this is often referred to as a <em>contract</em>. An instance of <code>*DirectoryRenderer</code> doesn't care which type of instance of <code>*DirectoryIterator</code> it receives, nor do instances of <code>*DirectoryIterator</code> care about how they're being rendered.</p> <p>Why? Because we've designed them that way. They just plug into each other and work. <em>This is OOP in action</em>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload