Jump to content

Classes And Includes


d_barszczak

Recommended Posts

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.php

include("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.
Link to comment
Share on other sites

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.)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Thanks ToonMariner

Ok 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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

[!--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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.