Jump to content


Photo

Classes And Includes


  • Please log in to reply
9 replies to this topic

#1 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 29 June 2006 - 08:53 AM

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.

<?php
// index.php

include("config.php");

$securitymanager->proccessLogin();



<?php
// config.php


// Load Classes
//////////////////

include("security.class.php");  // Loads the security manager.




Now if i include the security.class.php into the index file everything works but this way i get an error

Fatal error: Call to a member function databaseConnect() on a non-object in D:\www\LocalUser\callman\index.php


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.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 29 June 2006 - 09:36 AM


try

<?php
require_once("a.php"); // this will include a.php
require_once("A.php"); // this will include a.php again on Windows! (PHP 4 only)
?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 29 June 2006 - 09:47 AM

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.)
follow me on twitter @PHPsycho

#4 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 29 June 2006 - 09:50 AM

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.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#5 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 29 June 2006 - 10:42 AM

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.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 11:49 AM

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.


Life's too short for arguing.

#7 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 29 June 2006 - 01:10 PM

hvle Please read the above!!

I have tried this and it still is not working....

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#8 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 01:37 PM

you do have to include config.php on every file if you do this.
Life's too short for arguing.

#9 d_barszczak

d_barszczak
  • Members
  • PipPipPip
  • Advanced Member
  • 188 posts
  • LocationBradford - UK

Posted 29 June 2006 - 01:45 PM

[!--quoteo(post=389248:date=Jun 29 2006, 02:37 PM:name=hvle)--][div class=\'quotetop\']QUOTE(hvle @ Jun 29 2006, 02:37 PM) View Post[/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.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd


#10 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 01:51 PM

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
Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users