1

When URI "/service/add" , "/service/update" hit.. I want to update $service class member of Service Class. Basically an persistent Object to handle below issue "Issue#101": .

Basically I want to update (adding/ updating / removing some array key i.e. services)) same $service array by calling different controller from Router.php.

How can I achieve this. As I want to update just $service array I am not storing them into database and fetching into other controller.

In this case:

  • Storing just $service array into database never an option. (calling model from controller)
  • Storing into session may lead data risk if it contains sensitive information and $service may be large object in some other scenario.

Issue#101: Issue is to retain $service state from Services Class across different controller.

Router.php

post('/service/add',ControllerService/service_add_function);
post('/service/update',ControllerService/service_update_function);

ControllerService.php

class ControllerService extends BaseController{
    service_add_function(){
        $service = getPostData();

        $instance = Service::getInstance();
        $addedService = $instance->addService($service);

        //issue#102:

    }

    service_update_function(){
        $service = getPostData();

        $instance = Services::getInstance();

// **issue #101**: getService returns null but I want to get $service value
// added from service_add_function() controller

        $oldService = $instance->getService()

        // modify $oldService values by validating $oldService[key]

        $newService = $instance->addService($oldService);

        //rendering service template with updated service list
        VIEW::render(service.tpl,$newService);

    }
}

Services.php

class Services{
    static $instance = NULL;
    $service = array();
    static function getInstance(){
        if($instance === NULL)
            self::$instance = new Services();
        return self::$instance
    }

    addService($service){
        // I will do service[key] validation
        $this->service = $service;
    }

    getService(){
        return $this->service;
    }
}

Problem Resolution with Design Patterns would be highly appreciated if such pattern persists Object state throughout lifecycle until object not destroyed manually rather destroyed when php script execution ends.

tereško
  • 58,060
  • 25
  • 98
  • 150
devzone
  • 49
  • 1
  • 7
  • http://php.net/manual/en/features.sessions.php – Mika Tuupola Feb 03 '15 at 07:25
  • singleton is an antipattern, please stop using it. – tereško Feb 03 '15 at 09:13
  • @tereško What can be other alternative? Can you suggest any? – KillABug Feb 03 '15 at 09:36
  • If you have a shared dependency, between multiple classes, then you should initialize it outside them and inject said dependency thought the constructor. – tereško Feb 03 '15 at 10:47
  • @tereško: can you elaborate this in current issue scenario. My main intention is to retain last state of "Class Services" As per your comment ` $app->serviceInstance = function(){ return Services::getInstance(); // Creates new Object }; ## Controller Invocation new ControllerService ($app->serviceInstance); class ControllerService extends BaseController{ $lastServices ; function ControllerService ($serviceInstance){ $this->lastService = $serviceInstance; //#1002 } } ` #1002: returns last state of Services Class Is this what you trying to explain ? – devzone Feb 03 '15 at 12:12

0 Answers0