开发者

Reusing a Class Throughout Codeigniter Controller

I am coding my first CodeIgniter application (very familiar with PHP, but not CI) and have the following setup:

I have a controller, Signup, that controls a signup process. Every function of the controller is the next step in the process. I have an object, Did, that I am currently loading as a library. This object's properties/variables are updated as the signup process moves along.

The issue I'm having is that the properties from one function 开发者_开发百科of the Signup controller do not carry over to the next function. It is as if the class is re-constructed with every function.

Is there a way to reuse the class throughout the controller without it having to be re-instantiated? I'd rather not have to serialize and store in a session, either.

Thanks in advance.


As always, there are many solutions to the same problem. Please disregard this if it doesn't fit well with your implementation.

Keeping the signup steps in an object is a good idea- however, every time you load a new page CI rebuilds all the objects. In order for data to persist it should be stored in the session, but that doesn't mean you have to be working with session variables in your controller.

How are you transferring data to your application? Is it via forms or ajax?

One way you can do it is by unserializing the object from the session and storing it as an object in your controller's constructor. That way you can still run $myObj->function() against it and use a $myObj->save() function to reserialize and store it.

Hope that helps!


The problem you are having is that you are depending on the in-memory state of your application to remain from request to request.

You're expecting your class to use the same instantiation of your Did object between requests.

This is not how PHP/HTTP works. Each request is handled individually and is it's own instance of your application. So each request creates a new Did object.

To persist the state you need to either use Sessions to carry information between requests, use a database to handle your persistent state, or a combination of both.

Codeigniter Sessions

Codeigniter Database Class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜