bitman Posted June 16, 2006 Share Posted June 16, 2006 Hello all.i want to develop a php application for my web site.i have decided to take the object-oriented approach since,well , since i like it..my problem is i am kind of new to web programming and missing some vital information.hope you guys can help me with these:1. how do i make sure a main class i write will be available in every one of the script pages?do i need to make it a session variable? and save it to the session every time i make a changeof the data? or maybe i need to include the loading of the data in every one of thescripts and pages of the application?2. i guess this is very similar to the first question - how do i open one db-connectionat the loading of the application and use it throughout the different pages of my site?is there any reason (security or such) not to keep the connection open, and reopenit with every query?3. how does php know when a user switches application? by the url? i guess my question is,if i have a link in my site, to a different site, how does it know not to pass the sessionvariables to that site when a user clicks the link?4. if i define a global variable, does it have the same scope as a session or application variable?i guess i missed the correct documentation since all my questions regard to scope in web application.i learned php from a php manual that contained full reference, but no guide...so if anyone will be willing to point me to the right documentation section i will be verygratefull..thanks. Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/ Share on other sites More sharing options...
obsidian Posted June 16, 2006 Share Posted June 16, 2006 bitman, good questions, especially to get a handle on when you're starting. this is the type of things that you need to have a grasp on to create successful web applications. i'll try to answer them as best i can, but i'm sure some of the other mods/admins will have more to add as well:1) The declaration of the class will have to be included on every page that uses that class. One great solution is simply to have a shared header file that includes all required documents for the whole site, and then simply have every file then include that header.2) Same method as above. if you open the database connection in a common include file, you only have to work with it in one location, but it will connect every page in which it is included.3) i'm not sure i entirely understand what you mean by "switching applications". if they are switching to another app on your server, the session will be the same if your domain is the same. unless you have declared otherwise, a session is valid for the current domain. so, if you switch to another domain, you end up with the session not being valid. any other restrictions based on location and such are up to you to determine within the code and logic yourself.4) defining a variable as global simply allows the current function, class, or other scope you are inside of to recognize those variables that have been declared outside of that scope. for instance: if you declare the variable $conn for your database connection, and a function needs that connection reference, you simply would have to either pass the variable to the function through a parameter, or you would declare that variable as "global" within that function.i hope this helps you somewhat. web programming is similar in some respects to application programming, but as far as these questions go, you'll notice that there is quite a difference between them. Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46271 Share on other sites More sharing options...
bitman Posted June 16, 2006 Author Share Posted June 16, 2006 Hey, thanks obsidian,your replies sure help.i still want to clarify something regarding the first question\answer.If i say what you suggest, that is include all my needs in the shared header files,i will be able to define my classes in every one of the variable.but my question was a little different. i want to have a shared 'instance' of thatclass. like a static\global general class instance that is shared for all pages.i think this is called a single tone in application programming..is there anyway to implement something like that with php?(this way if my db connection resource is part of that class, i dont needto connect every time as well).p.s.how much posts do you have to post to get to super-guru??? Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46353 Share on other sites More sharing options...
.josh Posted June 16, 2006 Share Posted June 16, 2006 well i was gonna throw in my 2 cents on some of these issues, but [!--quoteo(post=384568:date=Jun 16 2006, 09:30 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 16 2006, 09:30 AM) [snapback]384568[/snapback][/div][div class=\'quotemain\'][!--quotec--]bitman, good questions, especially to get a handle on when you're starting. this is the type of things that you need to have a grasp on to create successful web applications. i'll try to answer them as best i can, [b]but i'm sure some of the other mods/admins will have more to add as well:[/b][/quote]i guess since i'm not a mod/admin i can't. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46358 Share on other sites More sharing options...
wildteen88 Posted June 16, 2006 Share Posted June 16, 2006 [!--quoteo(post=384659:date=Jun 16 2006, 06:08 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 16 2006, 06:08 PM) [snapback]384659[/snapback][/div][div class=\'quotemain\'][!--quotec--]well i was gonna throw in my 2 cents on some of these issues, but i guess since i'm not a mod/admin i can't. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /][/quote]huh? You can just reply to this thread! That was what obsidian was saying. Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46401 Share on other sites More sharing options...
trq Posted June 16, 2006 Share Posted June 16, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]i think this is called a single tone in application programming..[/quote]Its called a singleton pattern, and yes, it can be achieved within php. A simple example (php5).[code]<?php class foo { private static $instance; public static function getinstance() { if (empty(self::$instance)) { self::$instance = new foo(); } return self::$instance; }}?>[/code]Now, to use foo you simply do...[code]<?php $foo = foo::getinstance();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46530 Share on other sites More sharing options...
bitman Posted June 17, 2006 Author Share Posted June 17, 2006 Thanks thorpe.thats what i was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46611 Share on other sites More sharing options...
bitman Posted June 17, 2006 Author Share Posted June 17, 2006 hey again all you generous helpers,i tried writing a test to see if this code really acts like i thought,and well, it doesnt really.my class is: [code]<?php// file singleton_class.phpclass singleton{ private static $instance; private $test; public static function GetInstance() { if (empty(self::$instance)) { self::$instance = new singleton(); echo 'get instance'; } return self::$instance; } public function settest($value) { $this->test = $value; } public function gettest() { return $this->test; }}?>[/code]and i have two pages:index.php:[code]<HTML><BODY><?phpinclude_once('singleton_class.php');$single = singleton::GetInstance();$single->settest("lalala");echo $single->gettest();?><a href="page2.php"> click </a></BODY></HTML>[/code]and page2.php:[code]<HTML><BODY><?phpinclude_once('singleton_class.php');$single = singleton::GetInstance();echo $single->gettest();?><b> this is page 2 </b></BODY></HTML>[/code]in both pages i get the line 'get instance' printed from inside the getinstance() method...which means that the instance is a new one in every page.i thought it will be the same instance, otherwise what is the value of making it static?i suppose i misunderstand something.. Quote Link to comment https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/#findComment-46619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.