Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Real world OOP example

Tags:

oop

php

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

like image 629
BDuelz Avatar asked Aug 27 '09 20:08

BDuelz


People also ask

What is OOP in PHP with example?

PHP What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.

What is OOP real life examples?

It is a mental component rather than a physical thing. Let's take an example of one of the OOPs concepts with real time examples: If you had a class called “Expensive Cars,” it could contain objects like Mercedes, BMW, Toyota, and so on. The price or speed of these autos could be one of its attributes (data).

Is PHP good for OOP?

Yes, it is almost always a good idea to use OOP.

Which OOPs concept is used in PHP?

Polymorphism − This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it take different number of arguments and can do different task.


2 Answers

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.

like image 127
Peter Bailey Avatar answered Sep 29 '22 00:09

Peter Bailey


Purchase a book like "PHP and Mysql everyday apps for Dummies".

Its old I know [2005] but it shows concepts of User Logins, Forum, Shopping Carts, etc in both Procedural and Object Oriented with Mysqli.

It helped me learn Object Oriented PHP, I studied it a lot. Well worth the money.

OOP is much like grouping bits of your program into reuseable pieces. Its not that hard to be honest with you its just the idea of packing your functions into classes.

Real world mini example of OOP stuff below:

CLASS DATABASE
CLASS SESSIONS
CLASS WEBFORMS
CLASS EMAIL

CLASS ACCOUNTS (Example Functions below)
FUNCTION SELECTACCOUNT
FUNCTION CHECKPASSWORD
FUNCTION CHECKUSERNAME
FUNCTION CREATEACCOUNT

I hope you keep at it, PHP 6 will be re-engineered to support OOP more than ever.

Good Luck!

like image 40
RockyBalboa Avatar answered Sep 29 '22 00:09

RockyBalboa