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-02-2009, 01:14 PM   #1 (permalink)
The Contributor
 
planepixel's Avatar
 
Join Date: Feb 2009
Posts: 28
Thanks: 10
planepixel is on a distinguished road
Default help with object code

hi I was reading php manual on object and classes and came across this code. i have no idea what this code is doing. i tried to figure it out but could not get anything from it.


PHP Code:
<?php
class db {
  
//...
  
private static $instance;
  
//...
  
public static function singleton() {
    if(!isset(
self::$instance)) {
      
// what is the purpose of following two lines
      
$c __CLASS__;
      
self::$instance = new $c();
      
// End of lines
    
}
    return 
self::$instance;
  }
  
//...
}
?>
the bold text is the main issue. could anybody please explain what this chunk of code is doing.
Thanks
planepixel is offline  
Reply With Quote
Old 02-02-2009, 01:39 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I hope this helps you understand the above code.

php Code:
class db
{
    /*
    Defines a private variable to be used within the class
    Must be static as we're using it in a static function.
    */

    private static $instance;
   
    /*
    Begin a static singleton function
    These are called like so: db::singleton();
    */

    public static function singleton()
    {
        /*
        Only fill the private variable if it hasn't been
        set on a previous occasion.
        */

        if(!isset(self::$instance))
        {
            /*
            __CLASS__ is a magic constant.
            It is a string that equals "db", in this case
            as that's the name of our current class.
            */

            $c = __CLASS__;
           
            /*
            Assign the value of the new object to our private
            variable.
            */

            self::$instance = new $c();
            // End of lines
        }
   
        /*
        Return the object that references this class.
        */

        return self::$instance;
    }
}

/* To be used like so, function() doesn't need to be static. */
db::singleton()->function();
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
planepixel (02-02-2009)
Old 02-02-2009, 02:09 PM   #3 (permalink)
The Contributor
 
planepixel's Avatar
 
Join Date: Feb 2009
Posts: 28
Thanks: 10
planepixel is on a distinguished road
Default

Thanks for help. Now i got the point.
planepixel is offline  
Reply With Quote
Old 02-02-2009, 03:01 PM   #4 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

please don't write your future code in that manor. It will just make everything more confusing for you. Yes it may work but you will regret it.
CoryMathews is offline  
Reply With Quote
Old 02-02-2009, 05:40 PM   #5 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Why not write directly:

PHP Code:
if(!self::$instance) {
    
self::$instance = new self();

That looks much more cleaner and descriptive to me.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 02-02-2009, 06:33 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Probably he got the code from a tutorial, and tried to figure out what it meant.
__________________
Tanax is offline  
Reply With Quote
Old 02-02-2009, 06:34 PM   #7 (permalink)
The Contributor
 
planepixel's Avatar
 
Join Date: Feb 2009
Posts: 28
Thanks: 10
planepixel is on a distinguished road
Default

i will keep it in my mind from now on.by the way is it just php tags or something else also?
planepixel is offline  
Reply With Quote
Old 02-02-2009, 09:19 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How do you mean by just PHP tags? And I write my singletons the way xenon mentioned.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-03-2009, 12:28 AM   #9 (permalink)
The Contributor
 
planepixel's Avatar
 
Join Date: Feb 2009
Posts: 28
Thanks: 10
planepixel is on a distinguished road
Default

by php tags i mean '<?php and ?>'.I am new so copy pasted above code from php.net because i did not knew what the code was doing so instead of modifying i just pasted as it is.
planepixel is offline  
Reply With Quote
Old 02-03-2009, 01:00 AM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

What about the PHP tags exactly? I am a little confused now
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-03-2009, 02:12 AM   #11 (permalink)
The Contributor
 
planepixel's Avatar
 
Join Date: Feb 2009
Posts: 28
Thanks: 10
planepixel is on a distinguished road
Default

CoryMathews
Quote:
please don't write your future code in that manor. It will just make everything more confusing for you. Yes it may work but you will regret it.

xenon
Quote:
Why not write directly:

PHP Code:
if(!self::$instance) {
self::$instance = new self();
}
That looks much more cleaner and descriptive to me.
because of above messages i thought that i made mistake of including <?php and ?> tags in the post.
planepixel is offline  
Reply With Quote
Old 02-03-2009, 11:00 AM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

No mistake, it's ok to post snippets of PHP (ie not whole files) without opening and closing PHP tags.
Salathe is offline  
Reply With Quote
Old 02-03-2009, 01:38 PM   #13 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

No i didn't mean how you posted it here that was fine. I meant that when you are writing the code for yourself later on using php like this just makes everything harder to read, and harder to debug. So its good to know what this code does but not good to write in teh same style.
CoryMathews 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 Clone Construct to Clone an Object Wildhoney Advanced PHP Programming 5 01-29-2013 11:51 AM
Tips to Improve Your Coding and Projects Village Idiot Tips & Tricks 45 12-03-2012 07:46 AM
Writing Clean Code Village Idiot Tips & Tricks 10 06-25-2012 12:35 PM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
Snipply.com - Code snippet website codyodell Show Off 27 04-13-2008 02:09 PM


All times are GMT. The time now is 07:27 PM.

 
     

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