Object in Singleton design pattern

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ewawong
Forum Newbie
Posts: 1
Joined: Mon Dec 14, 2009 8:04 pm

Object in Singleton design pattern

Post by ewawong »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi Guys,

i am a newbie of php programming, i just started php programming for a week a go.

i tried to create a singleton object in php, but i am facing some problem.

here is my code

Code: Select all

 
<?php
 
require_once '../util/DbConn.php';
 
 
class SearchEngine
{
    
    private static $m_pInstance;
    private $titleArray;
    private $metaKeyArray;
 
    
    private function __construct() 
    { 
        echo "Constructor called<br />\n"; 
        
        $DbConn = new DbConn;
    
        $DbConn->connect();
        
        
        
        $result = $DbConn->getResult("SELECT itemid, title, metakey FROM jos_sobi2_item j");
        
        while($row = mysql_fetch_array($result))
        {
            //echo $row["itemid"]."<br>";
            //echo $row["title"]."<br>";
            //echo $row["metakey"]."<br>";
            
            $titleArray[$row["itemid"]] = $row["title"];
            
            $metaKeyArray[$row["itemid"]] = $row["metakey"];
        }
        
        echo $titleArray['5']."<br/>";
        echo $metaKeyArray['5']."<br/>";
        
        $DbConn->disconnect();
        
        echo "end of constructor";
        
    }
 
    
    public static function getInstance()
    {
        if (!self::$m_pInstance)
        {
            self::$m_pInstance = new SearchEngine();
        }
 
        return self::$m_pInstance;
    }
    
    
    public function query()
    {
        echo "message from query method<br />\n";
    }
    
}
 
 
function testFunction()
{
 
    $se = SearchEngine::getInstance();
    $se->query();
    echo "end of test method";
}
 
// Start the test
testFunction();
testFunction();
testFunction();
echo "after test method";
 
 
 
?>
 

after i run this file, all the code inside the constructor is able to be execute(select data from db, put in to array, print out from array, and echo the word of "end of constructor"), but after the the constructor is executed, other code is not running, it seems it stuck inside the constructor.........


so do you guys have any idea on what went wrong inside my code?


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Object in Singleton design pattern

Post by pickle »

Does it give you no errors?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Object in Singleton design pattern

Post by McInfo »

In the constructor,

Code: Select all

$titleArray
$metaKeyArray
should be

Code: Select all

$this->titleArray
$this->metaKeyArray
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 3:45 pm, edited 1 time in total.
nimeshrmr
Forum Newbie
Posts: 2
Joined: Sun Dec 20, 2009 9:41 pm

Re: Object in Singleton design pattern

Post by nimeshrmr »

You cannot access class variables using the variable name. You should use $this keyword

$this->titleArray and like wise inside the constructor.

for more details visit http://rakhithanimesh.weblog.com
Post Reply