Download Horizon :: Staff Members :: Save Vault :: XboxMB YouTube


Old 07-13-2012   #1 (permalink)
David's Avatar
Join Date: Sep 2010
Location: England
Posts: 3,506
Thanks: 3,156
Default Simple OOP Tutorial

Well I see a lot of people who don't use Object Oriented PHP so this tutorial isn't really a proper tutorial but more of a insight into OOP. Basically OOP is the future in my opinion but it hasn't always been there. It was introduced in PHP5 and has rapidly grown.

Now here is a simple example.

PHP Code:
<?php

class user {
    
    
// ------------------------------------------------------------------------------- 
    //  PRIVATE VARS 
    // ------------------------------------------------------------------------------- 
    
private $username;
    private 
$email;
    private 
$firstName;
    private 
$lastName;
    
    
/** 
     * __construct() 
     * Sets the user details when class is initilized
     *
     * @param string $username
     * @param string $email
     * @param string $firstName
     * @param string $lastName
     */ 
    
function __construct($username$email$firstName$lastName)
    {
        
// Seting the private variables
        
$this->username $username;
        
$this->email $email;
        
$this->firstName $firstName;
        
$this->lastName $lastName;
    }
    
/** 
     * user/returnUserDetails 
     * function will pull user info from our private vars and return them.
     * 
     * @return array 
     */ 
    
function returnUserDetails()
    {
         
// Stores user details from the private vars in an array
        
$details =  array(
            
'username' => $this->username,
            
'email' => $this->email,
            
'firstName' => $this->firstName,
            
'lastName' => $this->lastName
        
);
         
// Returns the array with all the user details
        
return $details;
    }
}
?>

That is a very simple class for storing user details and accessing them. Now time to show you how to use the class.
PHP Code:
// Initilizes the 'user' class and stores the instance in this variable
$userClass = new user('John''example@hotmail.com''John''Smith');
// Stores the user details which was returned from the 'returnUserDtails' function in the 'user' class
$user $userClass->returnUserDetails();

// Prints the array onto the document
echo '<pre/>';
print_r($user); 

Here is how you print one object of the array like username for example.
PHP Code:
// Initilizes the 'user' class and stores the instance in this variable
$userClass = new user('John''example@hotmail.com''John''Smith');
// Stores the user details which was returned from the 'returnUserDtails' function in the 'user' class
$user $userClass->returnUserDetails();

echo 
$user['username']; 

This may seem over complicated over something so small but when you have a large project this saves so much time, easier to read and just generally is a lot better than using the old procedural methods like this for example



Credits for MySQL class - Real-World OOP With PHP and MySQL | Nettuts+
__________________
David is offline Send a message via AIM to David
Reply With Quote
The following users thanked this post: Hero, imnotanoob, Jimmy, sgt frankieboy, UKz Prodigy


Old 07-13-2012   #2 (permalink)
sgt frankieboy's Avatar
Visual Bounds
Join Date: Nov 2010
Location: Netherlands
Posts: 3,755
Thanks: 3,205
Default Re: Simple OOP Tutorial

Look at dat.

Now what if I want to actually fetch the user details from a Database?
Care to make tutorial on that? ;)
__________________

XboxMB Chat


Programming Resources

Quote:
Originally Posted by Rich Cook
Programming today is a race between software engineers striving to build bigger and better
idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning.
sgt frankieboy is offline Send a message via AIM to sgt frankieboy Send a message via Yahoo to sgt frankieboy
Reply With Quote


Old 07-13-2012   #3 (permalink)
Regular Member
Jimmy's Avatar
Join Date: Nov 2010
Location: Shrek Republic
Posts: 1,847
Thanks: 929
Default Re: Simple OOP Tutorial

Thanks, the storing user details really helped me
__________________



big chief
Jimmy is offline Send a message via AIM to Jimmy Send a message via MSN to Jimmy
Reply With Quote




Old 07-13-2012   #4 (permalink)
David's Avatar
Join Date: Sep 2010
Location: England
Posts: 3,506
Thanks: 3,156
Default Re: Simple OOP Tutorial

Quote:
Originally Posted by sgt frankieboy View Post
Look at dat.

Now what if I want to actually fetch the user details from a Database?
Care to make tutorial on that?
Sure. Would help a lot of new people to PHP as I have noticed that they like to create login scripts and stuff like that which requires database interaction.
__________________
David is offline Send a message via AIM to David
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:52 PM.


 

Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
COPYRIGHT (c) 2010 - 2013 - XboxMB - DESIGN BY:
EDENWEBS.COM