Welcome Guest, Not a member yet? Register   Sign In
PHP objects
#1

[eluser]ndriana[/eluser]
Hi,

I am fairly new to MVC and ci in general. I don't understand why the view can change a model object. I think that it is logical that View is just the place where the templates are located.

But when I create a model object, say the header of the page (logo, horizontal nav, selected menu...) in the Controller, I(or the webdesigner) can change it when it is passed to View. Googling told that since PHP5, when you pass an object, it is always by reference.

But if it is changed in the view, my whole buisness logic in my Controller can be corrupted.

Namespace sounds a solution but I don't know how to do it.
#2

[eluser]WanWizard[/eluser]
This can indeed an issue if you work in teams and/or with designers.

Ideally you would use a presenter for this, which is an abstraction layer between the controller and the view. Jamie's blog might give you some pointers.

Alternatively, you can use a template engine (like Smarty, Twig, ...) which can also protect you from these changes, as they don't allow PHP to be used in the templates.
#3

[eluser]TWP Marketing[/eluser]
[quote author="ndriana" date="1348354389"]Hi,

I am fairly new to MVC and ci in general. I don't understand why the view can change a model object. I think that it is logical that View is just the place where the templates are located.

But when I create a model object, say the header of the page (logo, horizontal nav, selected menu...) in the Controller, I(or the webdesigner) can change it when it is passed to View. Googling told that since PHP5, when you pass an object, it is always by reference.

But if it is changed in the view, my whole buisness logic in my Controller can be corrupted.

Namespace sounds a solution but I don't know how to do it.[/quote]

Well, like the doctor said, don't do that. Don't change a value in the view. Move that ability into your Controller and/or Model. The only way a view "should" effect your data is if you send data back to the Controller for processing, by placing an html link or send form data back in a POST or GET array. And even then, your code controls how that information is handled upon reading the input data.
When your view data is submitted (via a form or url), YOU control the code in the controller(s) and model(s) which process that data.

Do read about the MVC paradyme. The "view" does not "create a model object", at least in the sense of an MVC model file. The "view" isn't "the place where the templates are located", view(s) are the actual html code files (with some parsed php if you need it) which is passed to the server for rendering. Strictly speaking, a view does no data processing/manipulation, but that is a grey area. Some views need to use php loops to properly display dynamic data. But they only display the data, not change it.

Controller directs the data flow
Model reads the database (and manipulates that data if necessary)
View displays the data and takes user input
#4

[eluser]ndriana[/eluser]
Thank you for the replies.
I understand that I should not but I'd prefer if I can't.

WanWizard> Jamie’s blog is really a good idea.

The only problem is the "require once" at the top of the file. It is a bad habit as I will have more and more models. It might be better to create libraries.

Edit: the following idea fails
Another idea that came to me after reading that blog is also to make all the class members protected, extend the class with handlers. And only the base class will be passed to the View. And the derived will be used in the Controller.
I can't access to protected properties outer the subclass. But if I make it public then the same problem remains.
Aggregation won't work either.
I guess the solution is that blog or make a copy of the data to a variable and pass that to the View.
Or the least expensive is to not think about it a be careful.




Theme © iAndrew 2016 - Forum software by © MyBB