Page 1 of 1

Object in Singleton design pattern

Posted: Tue Dec 15, 2009 2:33 am
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.

Re: Object in Singleton design pattern

Posted: Tue Dec 15, 2009 9:52 am
by pickle
Does it give you no errors?

Re: Object in Singleton design pattern

Posted: Tue Dec 15, 2009 10:54 am
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.

Re: Object in Singleton design pattern

Posted: Sun Dec 20, 2009 9:53 pm
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