Welcome Guest, Not a member yet? Register   Sign In
HMVC Variable Collision?
#1

Hello,

In CI3,
I have a page table that displays three columns of data in my site footer template it uses variable $row.
Example: $row->content

My main content also fills from page and uses variable $row

if I do not change $row in main content, it renders wrong content.

Am I using HMVC wrong?
What is the best way to avoid this?
It is time consuming to review all modules and variables used.

Thanks
Reply
#2

If you use the same var multiple it will always be overwritten by the last change. You should simple call it differnet, like $row_main, $row_footer ...

Reply
#3

The best way to avoid this is to be explicit both in naming your variables and in passing data to your views. In the end, though, this could have as much to do with how your template is constructed as with HMVC.

If you have a lot of data to pass to your template which you don't want shared with your content areas, you may want to consider coming up with a variable naming system similar to namespacing in JavaScript: creating a single associative array within your $data array which contains all of the variables for your template.
Reply
#4

BTW, this is a PHP issue not CI or HMVC. When passing data to a view, it runs extract() on the associative data array passed to the view which takes the data array and breaks it into individual variables. The variables get created in the global symbol table, not the individual view, since that's what extract() does. So yes, don't have any keys containing the same name in your data arrays being passed the views, they should all be unique.
Reply
#5

Thanks everyone for clarifying this. I will be more careful now. I may prefix the variables with the table name.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB