Welcome Guest, Not a member yet? Register   Sign In
Persistant Objects
#1

[eluser]alexis25[/eluser]
Hi,

I'm converting (for the fun, have fallen in love with CI) a PHP crapware application that my company built in the dark ages, taking some inspiration from Spring/Java (day to day stuff) I've implemented a form of flows to my application (i recommend this aproach, makes it very easy to organise views).

My problem comes later on when transferring from a view to a controller. I want my 'user' object (for instance) to persist, so that I can compare against values previously stored. however each time i return to my controller from a view, the objects are re-created, presumably garbaged as they should be when the script finishes and the views are generated.

I realise this is not perfect practice for PHP or OO or MVC, I'm trying to do something a bit different and don't particularly want to store anything in the session.

Anyone got any ideas?

Thanks
#2

[eluser]WanWizard[/eluser]
Are you saying that you create variables in your views that you want to pass back to the controller?
Or do you mean persistence across page requests?
#3

[eluser]alexis25[/eluser]
ok here's an example:

Class user {

public username;

function setUsername($username){
$this->username = $username;
}

function getUsername(){
return $this->username;
}
}

This class (a user library) is instantiated within the constructor of the master controller.

I use a login/userlogin function to set the username, then load to a different view. second view has a new form (form loads index.php/controller/function), calling a different function in the controller. when I get try and use $this->getUsername() from this new function, the username has been destroyed.

I want to stay away from passing $data vars between views, as this makes life very difficult for flow functions (and to me, looks messy), i'd also like to stay away from cookies/sessions, as i'd like for the application to spit just html, so that any device can access. I'd like to just have access to the $CI instance (my libraries/views will use the $CI =& get_instance() func) so that I can then get vars from the already instantiated object.

realise that's a long list of 'likes'! and that i may have to consider a different approach. any ideas welcome Smile
#4

[eluser]alexis25[/eluser]
so, workflow:

1x constructor
2x views
1x lib

controller1 loads lib1
controller1func1 loads view1
view1 sets lib1
view1 calls controller1
controller1 loads view2
view2 loads controller1func2 ----- here's the problem!!

when I get to the constructor1func2, having passed the 'user' object to view1 and to view2 succesfully, func2 doesn't have the lib1 object.
#5

[eluser]WanWizard[/eluser]
That is because views are intended to generate output, not to contain application logic (other then generating the output).
So when you load a library in a view, it is not assigned to a class variable of controller1.
#6

[eluser]alexis25[/eluser]
Absolutely, what I'm looking for is an idea for how to get round this behaviour. In any other language, the objects might persist until the application is sure that they are no longer needed.

Am I going to have to pass these via the session do you think?

I suppose that I could output the object variables to the session, then re-instantiate each time, but I'd like a form of keeping data alive without relying on user-based cookies, databases (to keep things simpler) or outputting to an XML file, do you think this is going to be impossible?
#7

[eluser]alexis25[/eluser]
sorry, re-read your post.

I realise that business logic should be maintained outside of views. what I'm looking for is a way to keep the logic's current state whilst the user decides on a course of action.
#8

[eluser]WanWizard[/eluser]
i'm clueless to what you're talking about.

A web application is stateless. You request a URI, and get HTML in return. While the user decides on a course of action, the PHP script has finished, all variables and objects destroyed. If the user has decided, and clicks on a link or presses a button, a new request goes to the webserver, which is not aware of the previous request, so it can't reference anything from that request. If you want to preserve state, you have to use server-side storage, and a client side identifier to be able to retrieve the correct data out of that storage. Aka sessions.
#9

[eluser]alexis25[/eluser]
fair enough, i guess i'm misunderstanding what I'm doing, I wanted to simplify the way that I store the session's available information, but you're absolutely right I'm going to have to do this server-side.

So to extend the discussion...

Can I store the object within the session?

for instance something like:

$this->userdata['user'] = $this->user;

Thanks again
#10

[eluser]WanWizard[/eluser]
See the Sessions section in the manual.

Objects are serialized when they are stored in the session record, so there are some restrictions, see the PHP manual.




Theme © iAndrew 2016 - Forum software by © MyBB