Lethe Posted March 17, 2009 Share Posted March 17, 2009 Ok, so I started programming with PHP for the first time (from ASP classic) and of course the first thing I needed to do is setup some global variables. Of course this is nowhere near as easy as I thought it would be. My research eventually lead me to the Singleton Pattern with means I now have learn about class, etc, etc... whoa a bit confusing to say the least. Not only that but there are several other methods for applying "sort-of" Singleton Patterns. Anyway the pattern version that made the most sense to me because it seemed the most flexible is - http://www.php.net/manual/en/language.oop5.patterns.php#89537 Which leads me to what I'm trying to achieve with this code. Basically I have a website template that starts with a user inputted unique id somewhere where it's easy to find and modify. This id is used to get more initial setup data from a DB which will be reused later in the template. There will also be a second separate DB connection that will again return more setup information for the template (yes, this is on purpose and I need it). now the questions 1. Can someone explain in plain english what the __construct() function does exactly and how it should be handled properly? 2. So using the pattern mentioned above I will put each of the DB connections and variables into 2 separate classes that both use a __construct() function? Will this work? 3. A single instance of each DB connection and variables will be made available the first time I call the module_loader with the name of class I want to use? 4. If I want a second instance (although I don't know why I would!) I just have to call the module_loader again and call it something else? 5. Why in this example has he used the module_loader to call a instance of the child class and not the parent (with the constructor function) instead? Does it matter? 6. Everybody seems to have their own ways and opinions about how to do this - so whats are the issues with doing it this way? 7. Is this code missing anything important that I should be aware of? The use of the destructor function for example. Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/ Share on other sites More sharing options...
Floydian Posted March 17, 2009 Share Posted March 17, 2009 I have no idea on questions >= 2, but on #1 the answer is: The __construct() method is called when you use the "new" keyword. So, when you make an instance of a class, the __construct() method is called allowing you to initialize your objects properties, or whatever else you might want initialized every time you use that class. The topic is labeled global vars (partially), but perhaps you might not know that global vars in PHP are known as superglobals. And there are a few of these available to you. $_SESSION, $_GET, $_POST, $_REQUEST To name a few of them. PHP is designed, IMHO, to be "anti-global". All functions and classes are isolated from all variables out of their scope except for the superglobals. So, if you don't want to use the superglobals as a means of freeing up a variable for use anywhere, I'd suggest having a class that you can carry around with you that stores your variables as public properties. Then you'd only need to use the "global" keyword to pull in that class, and all your variables with it. Personally. since PHP only has single inheritance, I use the __constructor of many of my classes to pull in other classes that I might need in the class. I'd then store those other classes as properties of the class I just instantiated. Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/#findComment-786484 Share on other sites More sharing options...
trq Posted March 17, 2009 Share Posted March 17, 2009 The topic is labeled global vars (partially), but perhaps you might not know that global vars in PHP are known as superglobals. And there are a few of these available to you. The op is likely looking for a php equivelent of asp's global.asa file. In this file you can simply load variables into the global namespace of your application. Seems nice, but like alot of things asp its a floored design. 1. Can someone explain in plain english what the __construct() function does exactly and how it should be handled properly? Classes are the same in php as they are in asp, they just have alot more features. So, I construct in php works exactly as it does in asp (vbsript). 2. So using the pattern mentioned above I will put each of the DB connections and variables into 2 separate classes that both use a __construct() function? Will this work? 3. A single instance of each DB connection and variables will be made available the first time I call the module_loader with the name of class I want to use? 4. If I want a second instance (although I don't know why I would!) I just have to call the module_loader again and call it something else? 5. Why in this example has he used the module_loader to call a instance of the child class and not the parent (with the constructor function) instead? Does it matter? You seem to have missed the entire point of the singleton pattern. The singleton pattern is designed so that there can only be 1 instance of a certain class. the examples provied shows this quite clearly. $instance1 and $instance2 refer to the same object. Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/#findComment-786505 Share on other sites More sharing options...
Lethe Posted March 17, 2009 Author Share Posted March 17, 2009 My fault - I was just a little confused by the way he referred to it as instance 1 and 2. So from this the answers to questions: 2 = yes 3 = yes 4 = no So what about question 5. I've run a test on this and either seems to work unless I've missed something. I just don't understand why he called the child class and not the parent? Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/#findComment-786511 Share on other sites More sharing options...
trq Posted March 17, 2009 Share Posted March 17, 2009 I just don't understand why he called the child class and not the parent? Because he wanted the child class. Children inherit from there parents, not the other way around. eg; there is usually more functionality within a child than there is a parent. Either way, if its an equivelent to asp's application object your after its more like a registry than a signleton. Of course a registry should likely be a singleton at the same time. A registry will allow you to store instances of objects as well as arrays and variables and have access to them application wide. Take a look at this tutorial on our main site. Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/#findComment-786514 Share on other sites More sharing options...
Lethe Posted March 17, 2009 Author Share Posted March 17, 2009 Okay, thanks, I think I get it. Sorry if my language is not up to scratch. Even though I've been using ASP for a long time it was before classes became "popular" so to speak. Mostly I've been working on code that was created before this time and not enough resources to change it. And even then using classes only degraded performance except in very specific usage. But I think this attitude towards classes is more a ASP community thing because its not as developed as the PHP community. Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/#findComment-786518 Share on other sites More sharing options...
trq Posted March 17, 2009 Share Posted March 17, 2009 Okay, thanks, I think I get it. Sorry if my language is not up to scratch. Even though I've been using ASP for a long time it was before classes became "popular" so to speak. Mostly I've been working on code that was created before this time and not enough resources to change it. And even then using classes only degraded performance except in very specific usage. But I think this attitude towards classes is more a ASP community thing because its not as developed as the PHP community. The thing with classes in asp is that they don't really give you any more flexability. They can help keep your code self contained, but you would have a hard time implementing most design patterns with them. There basically just glorified arrays with methods. You can't even dynamically instantiate an object in vbscript because of the languages constraints. Coming form an asp background myself I can tell you now that allot of what you learnt in asp can be done better and more efficiently in php. You probably shouldn't be trying to design your php applications around the same sorts of logic you would with asp. Theres likely a better way. Like I said before 'its easy to write bad code in asp'. The language itself doesn't give you the functionality to write good code most of the time. Link to comment https://forums.phpfreaks.com/topic/149775-solved-help-with-global-variablessingleton/#findComment-786657 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.