TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-24-2009, 10:25 PM   #1 (permalink)
The Wanderer
 
DizzyD's Avatar
 
Join Date: Feb 2009
Posts: 11
Thanks: 1
DizzyD is on a distinguished road
Help Object Oriented Programming

Hello Everyone!

I'm new to the community but not to php :). I've been in web development for roughly 12 years and more specifically programming in php for about 6. I haven't really dabbled in any other language as all work I've done has been in php :D. With that said I'd really like to get more into object oriented programming seeing as I'm pretty old school atm. I roughly get the basics of it w/ classes and functions/methods and variables/properties, but what I'm having a hard time grasping is the thought process. It's a whole different way of looking at it...

So is there anyone that can point me in the right direction that could help me understand the concept of this type of programming? Or even better if someone has an uber simple script that is class based that I can look at and physically understand instead of reading about. I asked a friend for a simple CRUD blog and he wrote an example using his own framework so it was 50million files... *sigh*

Sorry for the lengthy post...

~DizzyD
DizzyD is offline  
Reply With Quote
Old 02-25-2009, 12:54 AM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 322
Thanks: 2
Enfernikus is on a distinguished road
Default

I'm pretty sure I gave you this one before my 50 million file framework..

*it's old*

PHP Code:
<?php

/**
 * NOTE TO SELF
 * 
 * Database access layer for MySQL
 * internal variables are labeled as follows for easy refernece to what they contain
 *     - $iNumber  = the i at the beggining of the variable stands for an integer within the variable
 *     - $szString = the sz at the beggining of the variable represents a string within the variable
 *  - $aArray   = the a at the beggining of the variable means that there is an array within the variable
 *
 * For ease of use publicy available variables DO NOT follow this convention
*/ 

class mysql
{
    
/**
     * Set up neccesary variables, including connection and query identification as well as store connection info
    */
    
private $iConnectionID;
    private 
$iQueryID;
    public  
$num = array();
    public  
$queryCount;
    public  
$queries = array();
    
        
/**
         *  Name:__construct [ PUBLIC ]
         *  Uses: Initializes database connection
        */
        
public function __construct()
        {    
            
//now populate our connection array and make the connection
            
$this->iConnectionID mysql_connect(configVars::MYSQL_HOST,configVars::MYSQL_USER,configVars::MYSQL_PASS) or die('Unable to connect');
                                   
mysql_select_db(configVars::MYSQL_DATA,$this->iConnectionID);
        }
        
        
/**
         *  Name: query [ PUBLIC ]
         *  Uses: extracts SQL statement and injects escapes variables via sprintf
        */
        
public function query()
        {
            
//collect our arguments 
            
$iFuncArgs func_num_args();
            
$aFuncArgs func_get_args();
            
            
//lets check if we're getting a call from __call
            
if(is_array($aFuncArgs[0]))
            {
                
$aFuncArgs $aFuncArgs[0];
                
$iFuncArgs count($aFuncArgs);
            }
            
            
//check if there is only an SQL statement - if so, query it and return a record.
            
if($iFuncArgs == 1)
            {
                
$this->__query($aFuncArgs[0]);
                
$this->__populate();
                return new 
Record($this->iQueryID,$this->iConnectionID);
            }
            
            
//multiple input, break it off to secure
            
$aSql array_splice($aFuncArgs,0,1);
            
$aArgs array_map('mysql_real_escape_string',$aFuncArgs);
            
$szSql call_user_func_array('sprintf',array_merge($aSql,$aArgs));
            
$this->__query($szSql);
            
$this->__populate();
            return new 
Record($this->iQueryID,$this->iConnectionID);
        }
        
        
/**
         * Name: __call [ PUBLIC ]
         * Uses: get specified ammount of data (IE: one, row, all);
        */
        
public function __call($function,$args)
        {
            
//run our query
            
$query $this->query($args);
            
            
//now lets see what they want
            
preg_match('/get(.*?)/',$function,$matches);
            
$match next($matches);
            switch(
$match)
            {
                case 
stristr($match,'one'):
                    return @
reset($query->fields); break;
                case 
stristr($match,'row'):
                    return 
$query->fields; break;
                case 
stristr($match,'all'):
                    
$i = -1;
                    while(!
$query->EOF)
                    {
                        
$ret[++$i] = $query->fields;
                    }
                    return 
$ret; break;
                    
            }
        }
        
        
/**
         * Name: __query [ PRIVATE ]
         * Uses: MySQL wrapper for mysql_query
        */
        
private function __query($sql)
        {
            
$this->iQueryID mysql_query($sql);
            ++
$this->queryCount;
            if(
configVars::DEBUGME)
            {
                
$this->queries[$this->queryCount] = $sql;
            }
        }
        
        
/**
         * Name: __populate [ PRIVATE ]
        */
        
private function __populate()
        {
            
$this->num['fields'] = @mysql_num_fields($this->iQueryID);
            
$this->num['rows']   = @mysql_num_rows($this->iQueryID);
        }
        
}

class 
Record
{
    
/**
     * connection, query, row numbers, current row number, assoc field array, end of file mark
    */
    
private $iQueryID;
    private 
$iConnectionID;
    private 
$rows;
    public 
$curRow;
    public 
$fields;
    public 
$EOF false;
    
        
/**
         * populate iQueryID, iConnectionID, rows, and fields
        */
        
public function __construct($iQueryID,$iConnectionID)
        {
            
$this->iQueryID $iQueryID;
            
$this->iConnectionID $iConnectionID;
            
$this->rows = @mysql_num_rows($iQueryID);
            
$this->fields = @mysql_fetch_assoc($iQueryID);
        }
        
        
/**
         * Keeps track of what row we're on and stops us from erroring off
        */
        
public function movenext()
        {
            
//check to make sure we've not reached the limit
            
if($this->curRow != ($this->rows 1))
            {
                
//populate fields and increase the count
                
$this->fields mysql_fetch_assoc($this->iQueryID);
                ++
$this->curRow;
                return;
            }
            
            
//limit reached, mark of end of file
            
$this->EOF true;
        }
        
        public function 
__get($what)
        {
            return 
$this->fields[$what];
        }
}
Enfernikus is offline  
Reply With Quote
Old 02-25-2009, 02:43 AM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 836
Thanks: 31
sketchMedia is on a distinguished road
Default

Firstly, welcome to the community!

As odd as it my seem, the reason behind OOP is to simplify complex tasks (amongst a myriad of other things), however I know from experience it doesnt seem that way when your trying to learn it. Especially when every example consists of pointless foo and bar classes! Which are utterly pointless and counter intuitive.

I'm quite tired atm, I really should be asleep so I wont write an example now (i'll have to restrain myself! I've written full tutorials before now at this time on a work night, not a good thing to do i find), however if i get a spare few minutes tomorrow i'll fudge something together, that is if someone else hasnt beaten me to it!

I will post this though, have a watch (the only php oop tutorials I could find)
http://www.killerphp.com/videos/obje...php-videos.php

One day the proverbial penny will drop, trust me!
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
Object Aggregation Jenski Advanced PHP Programming 2 08-07-2008 09:53 AM
Object methods suddenly lagging? RobertK Advanced PHP Programming 9 01-11-2008 12:38 PM
12 common programming mistakes to avoid sunilbhatia79 General 0 11-16-2007 04:59 PM
Using the Clone Construct to Clone an Object Wildhoney Advanced PHP Programming 2 11-16-2007 02:39 AM


All times are GMT. The time now is 04:54 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design