jquery - PHP, Singleton and $.ajax -
it seems $.ajax (jquery) doesn't work php singleton.
i have simple class defined this:
class mysingleton { protected static $instance = null; private $array; protected function __construct() { ... $this->array = array(); //get database, $this->array[] = object database; $this->array[] = object database; ... } protected function __clone() { } public static function getinstance() { if (!isset(static::$instance)) { static::$instance = new static; } return static::$instance; } public function somefunction() { $this->array[0]->somefield = "set without saving database"; ... } }
i have helper.php file singleton object something. ie:
<?php require "mysingleton.php"; $singleton = mysingleton::getinstance(); $singleton->somefunction(); $singleton->someotherfunction(); ?>
in index.php, tried use $.ajax me:
$.each(data, function(key, value) { $.ajax({ url: 'helper.php', type: 'post', data: somedata, datatype: 'json' }).always(function(result) { ... }); });//each
as can see in jquery code, have called $.ajax few times.
i traced mysingleton, , instead of returning same instance, created few times (depending on $.each loop size).
i have read article: http://www.daniweb.com/web-development/php/threads/393405/php-singletone-pattern-in-php-files-where-ajaxs-requests-are-sent
and happens because of singleton pattern work during same request. in case have few ajax requests (again, based on $.each loop) , that's why never worked.
the reason using singleton object because don't want make multiple database connections, mysingleton have array (which used store objects) , in mysingleton class use array temporary store information without saving database)
so there anyway solve problem? want use $.ajax , php singleton.
the way save data between requests storing somewhere. means either session or file or in database.
i don't think loading data @ once slower loading 1 record, because 90% if loading time creating request, creating database connection etc. how try load data @ once , if slow can add cache or else on top of it, pretty sure fast enought.
Comments
Post a Comment