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 (3) Thread Tools Search this Thread Display Modes
Old 10-30-2010, 12:13 PM   #21 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
roman is on a distinguished road
Help

Hi,

I am trying to use singleton in my application. I believed singleton object can hold public variables to be used at any place of my code. But it does not work...
Thanks to anyone who can help me :)

PHP Code:


// singleton to keep errors
class Errors {
    
    private static 
$_instance;
    public 
$errorList;
    
    private function 
__construct () {
        
$this->errorList = array();
    }
    
    public static function 
getInstance() {
        if (empty(
self::$_instance))
            
self::$_instance = new Errors ();
        return 
self::$_instance;
    }
    
    public function 
addError ($error) {
        
$this->errorList[] = $error;
    }                
}

// when I use the singleton first time, it holds the public value of errorList
$err Errors::getInstance();
$err->addError('some error');
print_r ($err->errorList);

// when I get the instance again, the errorList seems to have lost it's value ('some error')
$err Errors::getInstance();
print_r ($err->errorList); 

Last edited by roman : 10-30-2010 at 02:05 PM.
roman is offline  
Reply With Quote
Old 10-30-2010, 06:49 PM   #22 (permalink)
The Visitor
 
Join Date: Jun 2010
Location: Hermosillo
Posts: 2
Thanks: 0
luisjba is on a distinguished road
Default

Hi Roman,

Your code is OK, but you need to know some about the HTTP environment and how PHP works.

When the browser request to your resource (your script in this case), the vars and clases in PHP are initialized,and a thread in the server is open to attend your request. Until the thread is ended, all your vars and temporary memory in the server are cleaned, so if you try to access your var in the next request, your vars doesn't exist anymore because them ware cleaned for ending of the Thread of the previous request.

To keep your vars for global, you can implement MemCache, you need to install the Mem Cache server, it allows you to allocate primitive vars and objects, and then retrieve them any time in any request, by this way, you keep the state of your var globally for all users.

I hope that this was helpfully for you.

Regards
Send a message via Skype™ to luisjba
luisjba is offline  
Reply With Quote
Old 10-31-2010, 09:21 PM   #23 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
roman is on a distinguished road
Smile

Thanks for your answer, Luisjba,

I understand that all vars exist within one request.

What I meant was that the singleton lost it's public values WITHIN the same request, as I showed in the code... I called the singleton twice - one after other - and the second call its empty... I am trying to use singleton in various scopes (functions) and still I fail to use it even within the same scope - that's why I do strange thing to call it twice, just to demonstrate how weird they behave..



Regads,

Roman

Last edited by roman : 10-31-2010 at 10:10 PM.
roman is offline  
Reply With Quote
Old 11-02-2010, 05:16 AM   #24 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 7
Thanks: 0
jgetner is on a distinguished road
Default

you shouldn't hijack this thread create a new thread witch would make it much easier for people to help you with your problem.
jgetner is offline  
Reply With Quote
Old 11-05-2010, 02:59 PM   #25 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
roman is on a distinguished road
Default

Sorry, I am inexperienced forum user. Will do it.
roman is offline  
Reply With Quote
Old 06-11-2012, 09:26 AM   #26 (permalink)
The Visitor
 
Join Date: Jun 2012
Posts: 1
Thanks: 0
mathewxiang is on a distinguished road
Default nice example

thanks for sharing this example about php singleton implement
mathewxiang is offline  
Reply With Quote
Old 10-18-2012, 09:43 AM   #27 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default

Some conservatives have Coach Factory Outlet pushed that critique further, saying that Mr. Obama’s policies are too costly, often assist the wrong people Louis Vuitton Belts and could have the paradoxical effect of driving up college costs. The dispute turns not just on different Coach Factory Outlet assessments of how policies play out, but on differing philosophical views about the role of government. During Gucci Belts his time in office, Mr. Obama has sharply increased aid to low- and middle-income students, notably through the Pell Grant Coach Factory Outlet program, which grew from $14.6 billion given to 6 million students in 2008, to nearly $40 billion for Coach Factory Outlet almost 10 million students this year. His administration also made it easier to request aid, shortening the Coach Factory Online complex federal application and allowing people to transfer their financial information electronically from the Internal Coach Outlet Online Revenue Service database. But while many education experts laud his efforts, analysts of varying political Coach Outlet Online stripes have also questioned how much impact some of the president’s policies will have, noting that the prices Coach Online Outlet charged by colleges, and student borrowing, continue to climb.But behind the headlines about soaring costs, the Coach Factory Outlet Online reality is more complex and wildly uneven, because a growing number of students receive Coach Outlet Online financial aid, and only relatively high-income families pay those fast-rising sticker prices. Adjusted for Coach Factory Online inflation, the College Board calculates, the average net price changed little over the last decade at private Coach Factory Outlet schools, and rose only modestly at public ones.Defending federal spending, Arne Duncan, the secretary of Hermes Belts education, said that for more than 30 years, college prices had risen even when federal aid had not, leading him to believe Coach Factory Online there was zero correlation.
dashixiong is offline  
Reply With Quote
Old 10-22-2012, 08:16 AM   #28 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default Coach Outlet

You’ve relativelyCoach Outlet recently arrived in New Delhi after living in two of Asia’s other great cities,Coach Outlet Store Online Tokyo and Hong Kong, for several years. Do these cities feel like they’re part of the same continent? Yes, and no. In terms Coach Factory Onlineof infrastructure, they couldn’t be more different. Getting regularCoach Outlet power and water at my house in New Delhi is never a sure thing, even though Coach Purse Outlet OnlineI’m paying the same rent that I paid in Tokyo and almost the same electricity prices. Both Hong Kong and Tokyo are also crowded places,Coach Factory Outlet Online but both cities are incredibly well planned and efficiently run. Efficient is not a word I would use to describe my Coach Bags Outlet Onlineday-to-day life in New Delhi. On the other hand, one thing that I think Hong Kong and New Delhi have in common isCoach Handbags Outlet a shared sense of optimism — a feeling that the best is yet to come. That’s definitely not the feeling you get in Tokyo,Coach Outlet Online or in the U.S. when I go home. It’s a big part of what I find addictive about living and working in this part of the world. You feel like you’re watching the future unfold.
dashixiong is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html
Posted By For Type Date
Building an Apache-like Access Control List (ACL) - TalkPHP This thread Refback 01-03-2008 06:10 AM
PHP Advanced PHP: How to use the Singleton Design Pattern Tutorial This thread Refback 12-22-2007 11:11 AM
PHP OOP Advanced PHP: How to use the Singleton Design Pattern Tutorial This thread Refback 12-21-2007 09:55 PM

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


All times are GMT. The time now is 07:21 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