<div dir="ltr">Hi Edward,<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 18, 2014 at 6:26 AM, Edward Cheng <span dir="ltr"><<a href="mailto:c4150221@gmail.com" target="_blank">c4150221@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi, Chanaka Dharmarathna,<br>
<br>
I'm sorry code I provided have problems,<br>
see sample blow, I fixed them<br>
<pre><br>
<?php<br>
header("Content-type:text/plain");<br>
<br>
class Sample<br>
{<br>
<br>
    private $selfData;<br>
    private $selfType;<br>
<br>
    private static $_instance;
<div><br>
    public static function getInstance($type)<br>
    {<br>
        if (is_null(self::$_instance)) {<br>
</div>            self::$_instance[$type] = new Sample($type);<br>
        } else {<br>
            if (! array_key_exists($type, self::$_instance)) {<br>
                self::$_instance[$type] = new Sample($type);<br>
            }<br>
        }<br>
        return self::$_instance[$type];<br>
    }<br></blockquote><div><br></div><div>Nice try.<br></div><div>This logic seems to be fine, since there are only two new objects are creating. shall we do following things to the current code.<br><br></div><div>1. Modify $_instance name to $_instances (or better; $_instanceArray ? )<br>

</div><div>2. Initialize it to empty array so that developers will not confuse. (normally singleton pattern keep single instance, but here it's array)<br></div><div>3. Apply above logic. (instead null check use array empty check since step 2)<br>

</div><div>4. Make constructor and $_instances array private. there are more variables you can make private.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">


<br>
    public function __construct($type)<br>
    {<br>
        $this->selfType = $type;<br>
    }<br>
    public function setData($data)<br>
    {<br>
        $this->selfData = $data;<br>
    }<br>
    public function getData()<br>
    {<br>
        return $this->selfData;<br>
    }<br>
}<br>
<br>
Sample::getInstance('A')->setData('AData');<br>
echo Sample::getInstance('A')->getData();<br>
Sample::getInstance('B')->setData('BData');<br>
echo Sample::getInstance('A')->getData();<br>
echo Sample::getInstance('B')->getData();<br>
<br>
</pre><br>
<br>
Results is "ADataADataBData", is that ok?<br></blockquote><div><br></div><div>Result is obvious. I was worrying about the requests from different users. But seems web server is managing it to run requests sequentially, which gathered into a queue.<br>

</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
2014-04-18 1:01 GMT+08:00 Chanaka Dharmarathna <<a href="mailto:pe.chanaka.ck@gmail.com" target="_blank">pe.chanaka.ck@gmail.com</a>>:<br>
<div><div>> On Thu, Apr 17, 2014 at 9:47 AM, Edward Cheng <<a href="mailto:c4150221@gmail.com" target="_blank">c4150221@gmail.com</a>> wrote:<br>
>><br>
>> Hi,<br>
>> I thought we have these code can avoid errors, in my new PR I improve it.<br>
>> <pre><br>
>> /**<br>
>>  * Returns class instance.<br>
>>  *<br>
>>  * @param string $type the table type<br>
>>  *<br>
>>  * @return PMA_RecentFavoriteTable<br>
>>  */<br>
>> public static function getInstance($type)<br>
>> {<br>
>>     if (is_null(self::$_instance)) {<br>
>>         self::$_instance[$type] = new PMA_RecentFavoriteTable($type);<br>
>>     } else {<br>
>>         if (self::$_instance[$type]->table_type != $type) {<br>
>>             self::$_instance[$type] = new PMA_RecentFavoriteTable($type);<br>
>>         }<br>
>>     }<br>
>>     return self::$_instance[$type];<br>
>> }<br>
>> </pre><br>
>> Does my PR fix it?<br>
>><br>
>> 2014-04-17 0:02 GMT+08:00 Chanaka Dharmarathna <<a href="mailto:pe.chanaka.ck@gmail.com" target="_blank">pe.chanaka.ck@gmail.com</a>>:<br>
>> > Hi,<br>
>> ><br>
>> > It seems like, the latest PMA_RecentFavoriteTable is trying to behave as<br>
>> > singleton design pattern. Is that a requirement ? I mean is there any<br>
>> > reason<br>
>> > for making it singleton ? By the way, there is good possibility to<br>
>> > create<br>
>> > new instances again and again with current logic.<br>
>> ><br>
>> > And the class has $table_type instance variable and more. Isn't it risky<br>
>> > behaviour to share a static instance throughout the web server, which<br>
>> > has<br>
>> > instance variable which used in functions of that instance ?<br>
>> ><br>
><br>
><br>
> Hi Edward,<br>
><br>
> Your code does not avoid creating many instances. By the way, we better<br>
> first clarify our self, the requirement. Any thoughts ?<br></div></div></blockquote></div><br clear="all"></div><div class="gmail_extra">Btw, if you are not posting inline, please use bottom posting ;)<br></div><div class="gmail_extra">

<br></div><div class="gmail_extra">Regards !<br></div><div class="gmail_extra">-- <br><div dir="ltr"><font size="4">Chanaka Dharmarathna<br></font><div><a href="http://chanakaindrajith.blogspot.com/" target="_blank"><font color="#999999"><b>http://chanakaindrajith.blogspot.com/</b></font></a></div>

</div>
</div></div></div>