![]() |
application-wide object
quick question -- i have a couple of core classes that "run" my application that i'd love to have persist over the entire application so that i can easily use it throughout the application without having to include and instantiate the class on every one of my controllers...
so, right now i have this: Code:
Code:
$_SESSION['site_core']->do_something();thanks everyone! robocop |
Would the registry pattern help?
PHP Code:
PHP Code:
PHP Code:
|
ok -- that could work...
my question is where would i put the code below? Code:
Registry::set('careers_core', new careersCore('site.php'));thanks for the help from across the pond! kasey |
I would put it in a 'bootstrap loader' (just like your computer), a file that is executed once per execution of the script that sets up your script.
In actual fact, it doesn't really matter where you do 'set', as long as its before you call the 'get' for that specific object. |
I need help with this also, I have $app->core->xss_clean(''); for example working, but i need it to be defined throughout the application (in included files etc). I want to be able to access it through $app. Is this possible.
Please note all files are loaded from index.php through functions and includes etc. At the moment im using Factory() but i really dont like it and would like to just use $app->CLASS->FUNCTION. Also is there a way to make this dynamic so that i dont have to do $app->core = new core(); Thanks |
1 Attachment(s)
How about using like the following to achieve what you're after? All you have to do is ensure the
TalkPHP_Factory is included on every single page, and then make your calls as so:php Code:
This is in the style Class->Function(). Is this something like what you're after?Obviously you'll need to modify it a touch to auto include files, but I added the functionality in the __autoload() function, just to show you how you could do it.php Code:
|
So is there no way of doing it with haveing $app. At the moment i use Factory('core')->xss_clean('')but it does not look good
|
Do you want to use the singleton on an individual basis like the following? Do you really want a factory, as I showed you above?
php Code:
This is the best way for it to be application wide. You just call getInstance() and there you have the instance. If you don't need many instances of the object, simply use it like so. |
Again that is similar to what i have. I idealy would like to have like a parent class of $app and all classes under that $app->class as its a framework im making so it needs to be simple code to use. I already have the $app->class->function() working but i need to make it so i dont have to keep doing $app = new app;. the reason for needing it is i use a lot of included files and classes in other classes
|
I see. Well, just use my original code and set that to
$pApp like so:php Code:
If you do that in a file that is loaded towards the beginning of the execution, then $pApp will be accessible from most places throughout your applications. You could make it a global, but I'm not too fond of that. Outside of classes and functions, it will work perfectly well like so, even when in different files:php Code:
|
My index.php (This needs to carry to:
All Other classes All Other files that are included by functions Everything that is included really without having to do PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
Sorry for the double post but i wanted to show what code I have been using to make it global untill now.
PHP Code:
Example usage of above: PHP Code:
|
Quote:
There is no way to make a variable, such as '$app' fully accessible like that (i.e. automatically available in other scopes) as far as im aware anyway, it will work great until you enter into another scope, then it wont be accessible (as you are no longer in the global scope). We can however use the global keyword to 'import' any variables defined in the global scope into our current scope. An example: PHP Code:
PHP Code:
I'm not sure if I understand your implementation of a singleton (or indeed most of it, seems very complex for a relatively simple problem in my opinion). I approve of the use of a factory method; as it allows you to access system objects without worrying about whether the class has been included (if its implemented to include the file needed) and/or pushed on the the system object array (i.e. registry), but is there anything stopping me doing this?: PHP Code:
Anywho..... A singleton object is an object that can be created only once, there shouldn't really be an external factory/registry implementation involved in order to make an object singleton. This is a classic singleton implementation: PHP Code:
PHP Code:
PHP Code:
PHP Code:
BTW, a few errors in your code (well notices if you read as PHP would) Wildhoney, a few static properties called in an objective context. Nothing however to scream about :-D PHP Code:
PHP Code:
|
That doesn't need to be static as it is called after the
getInstance() call. Therefore it is now an object, as it were.Nonetheless, a very good post! Another way to do this, although I don't approve of this way, is to use $GLOBALS. I've wrapped it in two functions just to make it look slightly more elegant, but not a lot!php Code:
|
Ok thanks for all the help guys, I have decided to use Kalle's way of doing it as he showed me on msn that its a good way. The class is:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
Thanks |
Quote:
Code:
|
Oh, I see.
$m_aInstances just shouldn't be set as static. |
| All times are GMT. The time now is 05:30 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0