Welcome Guest, Not a member yet? Register   Sign In
Persist an object and its methods
#1

[eluser]Jim Higgins[/eluser]
I am wondering if it is possible to persist an object beyond the controller or model it's created in. I know, for instance, you can pass an object to a view, but I believe the object is simply turned into an array. Can you maintain the object and it's methods when passing an object to a view?

In other words, can I create an object in a controller, pass that object to the view, access it's methods, and then pass it on to another controller if I wanted?
#2

[eluser]Sam Dark[/eluser]
You can pass it to view and use it's methods. But you can't call another controller from currently running one.
#3

[eluser]Jim Higgins[/eluser]
Is it also possible to pass an object to a controller? Rather than re-create an object?
#4

[eluser]Sam Dark[/eluser]
What do you mean by pass an object to a controller?

1. You have only one controller at one time.
2. You can't call additional one.
#5

[eluser]Jim Higgins[/eluser]
Not trying to call multiple controllers. Just asking, if I pass an object to a view, I can not then pass the same object into a function of a controller then? If I create an object, then load a view, then call a controller from the view, I need to recreate the same object again within the controller before I can use it?
#6

[eluser]Sam Dark[/eluser]
Yes. It's PHP. No persistent objects like in java.

p.s. if you meant view partials or sub-controllers within views, it's possible.
#7

[eluser]Jim Higgins[/eluser]
Okay. That's my problem... I am new to PHP. I have a lot of experience with OOP using Flash ActionScript as well as Java. Was trying to figure out a way to keep my objects alive from one stage of the application to the next.

I was trying to avoid recreating search results each time a user came to a page...

Controller creates an array of search results and displays them in a view
User clicks one result and goes to a different view
The user wants to go back to the search results

I didn't want to, essentially, re-run the search and recreate the objects.

Thanks for your help. Much appreciated.
#8

[eluser]Rick Jolly[/eluser]
[quote author="Jim Higgins" date="1206658595"]Okay. That's my problem... I am new to PHP. I have a lot of experience with OOP using Flash ActionScript as well as Java. Was trying to figure out a way to keep my objects alive from one stage of the application to the next.

I was trying to avoid recreating search results each time a user came to a page...

Controller creates an array of search results and displays them in a view
User clicks one result and goes to a different view
The user wants to go back to the search results

I didn't want to, essentially, re-run the search and recreate the objects.

Thanks for your help. Much appreciated.[/quote]

What you're describing isn't possible in any web application. The web is stateless, so every user action (that isn't handled completely client-side with javascript) requires another round trip to the server. Even in a java web application you'd need to unserialize data (either from database or file) and recreate objects for every http request.

Now you could store the search results in the session (edit: or the parameters necessary to recreate the search results), but you'd still have to unserialize that data from session storage (cookie, file, or database) on every http request.
#9

[eluser]Sam Dark[/eluser]
[quote author="Rick Jolly" date="1206665669"]
What you're describing isn't possible in any web application. The web is stateless, so every user action (that isn't handled completely client-side with javascript) requires another round trip to the server. Even in a java web application you'd need to unserialize data (either from database or file) and recreate objects for every http request.[/quote]
You are wrong. What you said can be applied to PHP, Perl but when it comes to Java it's stateful.
#10

[eluser]nmweb[/eluser]
Take care in php4 that you should pass objects explicitly by reference. It's counterintuitive, bloody annoying and fixed in php5 but if you're coming from a Java background and you don't know about it, you might run into some weird bugs.




Theme © iAndrew 2016 - Forum software by © MyBB