d_barszczak Posted June 29, 2006 Share Posted June 29, 2006 Hi,I have one file called index.php which loads a file called config.php via the include command.In the config.php file i want to be able to load my php classes that i have developed for my app so that the index.php file could use any of the classes i have created.[code]<?php// index.phpinclude("config.php");$securitymanager->proccessLogin();[/code][code]<?php// config.php// Load Classes//////////////////include("security.class.php"); // Loads the security manager.[/code]Now if i include the security.class.php into the index file everything works but this way i get an error[b]Fatal error: Call to a member function databaseConnect() on a non-object in D:\www\LocalUser\callman\index.php[/b]Is there any way i can do what im wanting as it would make my coding life easier if i could use a config file as opposed to including all my classes on every page. Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/ Share on other sites More sharing options...
redarrow Posted June 29, 2006 Share Posted June 29, 2006 try<?phprequire_once("a.php"); // this will include a.phprequire_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)?> Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50745 Share on other sites More sharing options...
ToonMariner Posted June 29, 2006 Share Posted June 29, 2006 First off I would alter your include to require_once.Secondly the error you are getting suggests you are calling a method from a class that has not been instantiated.Some where in your code you are missing $class = new classname; (replace these with your own names of vars and classes.) Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50746 Share on other sites More sharing options...
d_barszczak Posted June 29, 2006 Author Share Posted June 29, 2006 Hi,Not sure if we got our wires crossed as im not wanting to include the same file twice i just want to include one file that then includes the rest of my files so that i should only have to include the config.php then that script will do the rest.If i include the files directly in the index.php it works fine but this way i would have to do this on every page. Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50749 Share on other sites More sharing options...
d_barszczak Posted June 29, 2006 Author Share Posted June 29, 2006 Thanks ToonMarinerOk basically i have have created my classes in seperate php files (For Organisation) and the end of each file i have the line as you suggested.eg. $securitymanager = new securitymanager();Now if i include (or require_once now) security.class.php directly into the index.php everthing works fine. I dont want to do this though i want config.php to include these files for me so that index.php (and any others) only have to include config.php and then all of the setting are stored in there. Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50763 Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 Well, that's exactly what these guys been trying to tell you. You need to use require_once instead of include and use this in your config.php file.Then you do not need to include any but config.php file in your index.php.NOTE:require_once is same include, except:- if the file to include is not found, script exit on error.- the file only evaluate once, so you will not have class redefinition errors or thing like that. Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50781 Share on other sites More sharing options...
d_barszczak Posted June 29, 2006 Author Share Posted June 29, 2006 hvle Please read the above!!I have tried this and it still is not working.... Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50808 Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 you do have to include config.php on every file if you do this. Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50816 Share on other sites More sharing options...
d_barszczak Posted June 29, 2006 Author Share Posted June 29, 2006 [!--quoteo(post=389248:date=Jun 29 2006, 02:37 PM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 29 2006, 02:37 PM) [snapback]389248[/snapback][/div][div class=\'quotemain\'][!--quotec--]you do have to include config.php on every file if you do this.[/quote]Yes i know.But the idea is that config.php will include any other files that i require so i only have to include one file on every page and not 6 or 7. Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50823 Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 If it still doesn't work, then something else must be wrong.Perhap you could provide some error or suspicous code which you think could be the error.the above suggestion is straight out from text book Quote Link to comment https://forums.phpfreaks.com/topic/13184-classes-and-includes/#findComment-50824 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.