Welcome Guest, Not a member yet? Register   Sign In
HELP! regarding variable scope in a library I created
#1

[eluser]steveh[/eluser]
Hi all...
In summary, I need some pointers/corrections regarding how to handle variable scope within a library I created. The problem seems to be that when I call (methods in) my library from a controller/view/etc, some data that I stored in an array in the library only appears when I later access the array library from the same view/controller.
What I was wanting to achieve is a generic library into which I can place various home grown utility functions, and data... not the least of which would be generalised handling of headers, footers and menus, etc... so I'd like to call the lib from various places during the processing of a Request... and use any data stored in an array in that lib for example later when I build a menu with the items I then know are needed (irrespective of which view/controller put it there).
I've called the library "Llib" (for Local Library), and I include 'llib' in the autoload[libraries] of autoload.php. I also included a __construct in Llib in which I call write_message(), which dutifully displays "Llib Class Initialized" at the same point that the other libraries are being loaded.
I hadn't progressed the app prototype far before I encountered this issue. Basically, I'd provided for a login_authentication controller/view - that, when submitted and successfully processed (using the CI session library), will redirect() to a main_page controller/view. I noticed that data stored in an array in Llib by the login_auth view (e.g. summary user details and state) was not available when accessed from the main_page view (only data I'd set via method calls from the main_page view was there).
I set and get the data via a couple of methods in Llib that access a var array ... declared similar to "class Llib { var $myArray = array(); ...
...the set and get functions a pretty basic, e.g. function set_item($item, $value) { $myArray[$item] = $value; }
...and to debug/verify what's in the array at any given time, I simply: foreach ( $myArray as $name => $val ) { log_message(debug...) }
Hence, I think I've encountered some sort of data scoping issue. I've read quite a lot about data scoping in PHP, however I seem to have lost the plot insofar as how it is managed within CI (for the Request, the different controllers/views, CI's super object.... arrrrrgh!). (I am way more familiar with Perl than PHP, so it may even be some basic PHP 101 thing that I'm just not used to).
I'd just like the array data to be considered Global to my Llib methods called from any controller/view during processing a particular request (i.e. the instance of Llib loaded that request).
Please let me know if I need to provide any more info.
Thanks in advance for any 'how to' on this one,
Steve


Messages In This Thread
HELP! regarding variable scope in a library I created - by El Forum - 04-25-2011, 07:28 AM
HELP! regarding variable scope in a library I created - by El Forum - 04-25-2011, 07:38 AM
HELP! regarding variable scope in a library I created - by El Forum - 04-25-2011, 10:04 AM
HELP! regarding variable scope in a library I created - by El Forum - 04-25-2011, 07:17 PM
HELP! regarding variable scope in a library I created - by El Forum - 04-25-2011, 08:47 PM
HELP! regarding variable scope in a library I created - by El Forum - 04-26-2011, 04:21 AM
HELP! regarding variable scope in a library I created - by El Forum - 04-26-2011, 06:05 AM
HELP! regarding variable scope in a library I created - by El Forum - 04-26-2011, 07:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB