PHP: Real world OOP example

Viewed 51063

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

8 Answers

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!

you're right 99% of the tutorials that you'll find online are too basic, and they don't make sense. My suggestion to anybody trying to learn object oriented programming is:

find some code that you've written in procedural programming and try to convert it into OOP.

Take all your variables and make them a properties of a class, find a suitable class name. Then take all your functions, if you have functions, and group them within a class. If you wrote your code without using functions, first try to convert your code into a group of functions. find suitable function names, a function should define what a piece of code does. Once you have your functions move them into a class.

I'll give you a simple example of a pagination code that I converted into a reusable pagination class. find the full tutorial on youtube: https://www.youtube.com/watch?v=X38IRlyY_ww&t=91s , link to the source code is on the description box

class Paginator{

    /*the class need to know the current page to calculate the offset
       the offset tell my sql how many rows to skip */
   private $current_page;
   //the limit is the number of results to display per page
   private $limit;
   /*pagination links to display below the results with next 
   and previous button*/
   private $pagination_links;
   //the last page of your pagination links 
   private $last_page;
   .
   . etc

    public function __contruct($number_of_rows_found,$results_to_display_per_page){

      //the paginator uses the number_of_rows_found to determine the last page 
       $this->last_page = $number_of_rows_found/$results_to_display_per_page;
          //get the current page, set it to 1 by default
         $this->current_page = isset($_GET['page']) ? ($_GET['page'] : 1;

    }

   public function generate_pagination_links(){ 
          this method uses the last_page property to generate pagination links
           if the last page is 3, this will give you 3 pagination links
         for ($page = 1; $page <= $this->last_page; $page++) {
            //use
         }
   }
public function get_offset_and_limit(){
     //this method calculates the offset based on the current page
      return "LIMIT $this->per_page OFFSET ".($this->page - 1) * $this->per_page;
 }
.
.
.etc
}
}

Then to use the pagination class, you create a new instance of it, and pass the number of results you want to display per page, and the the number of results returned by your query as parameters. The pagination class will generate pagination links for you and calculate the offset and limit. That's a good example of a php reusable class, you can use it in your multiple projects without having to rewrite or change it.

$paginator = New Paginator($rows_found,8);
    $pagination_links = $paginator->get_pagination_links();

    $offset_and_limit =  $paginator->get_offset_and_limit();

    //apend the limit and offset to your sql query
    $query = $query. ' '.$offset_and_limit;

    $connection = getdbconnection();
    $stmt = $connection->prepare($query);
    $stmt->execute();
    $movies = $stmt->fetchAll();

Ofcourse there're more advanced concepts into OOP that are not covered in this example, but this should give you a basic understanding of how classes and objects work

Related