Jump to content

[SOLVED] Includes within includes within includes


sennetta

Recommended Posts

I'm making use of inheritance capabilities offered in PHP5.  I'm including the parent class (using the php require() function) at the head of the child class, and then going on to write the child class, and saving all classes as .php files.  Firstly, is this the standard practice in php? I'm used to Java importing packages rather than including files...

 

Also this is causing problems when the classes are accessed from different directories.  I have a site and there's an admin directory (www.mysite.com/admin) and a lib directory which contains all my classes (www.mysite.com/lib). 

 

From within the admin dir, I use a class "Bar" from by including "Bar.php":

<?php
include("../lib/Bar.php");
$barObj = new Bar();

// continue with proceedual, html-output type stuff here.....
?>

 

Bar itself inherits from a class called "Foo" which also resides in the lib directory.  So, at the top of "Bar":

<?php
include("Foo.php");

class Bar extends Foo {

  // class content
}
?>

 

The trouble is there is a file called "foo.php" (with a small letter, as it isn't a class) in the admin directory, and the include function in the Bar class is including both these files even though the file-names are different.

 

It says in the documentation (http://uk2.php.net/include/) that the file is looked for "in the current working directory" (in my case "/admin"?), then in the directory of the current script (presumably "/lib"?), and so both the files are being included, despite me explicit asking for "Foo.php" with a capital letter.

 

I only want what I ask for to be included, not this random page which happens to have a similar (not identical) file name.  Are there any workarounds for this any one can suggest?

 

Many thanks,

 

Anthony

Link to comment
Share on other sites

No its not really an OOP problem.

 

Include files should be included relative to the original page being called. This may be causing your issue. I always use full paths for my includes.

 

You might also take a look at using the __autoload function. Used in conjunction with set_include_path can also help.

 

PS: If you have classes including classes all over the place too, its probably safest to use include_once. Even if you include a file within a classes definition they enter the global scope.

Link to comment
Share on other sites

What the __autoload functiion? I use it pretty well exclusively, though I may hard code classes that I know will be required on every request.

 

Theres some cool tricks can be done with the __autload function too. You can almost get the impression of having namspaces, well, not really but you can organise things allot better.

 

Say you have a file structure.

 

[pre]

/

  index.php

  inc/

    db/

      foo.class.php

    util/

      foo.class.php

 

[/pre]

 

 

db/foo.class.php

<?php class db_foo {} ?>

 

util/foo.class.php

<?php  class util_foo {} ?>

 

index.php

<?php

  function __autoload($class) {
    $class = str_replace('_','/',$class);
    if (file_exists('inc/' . $class . '.class.php');
      require_once 'inc/' . $class . '.class.php';
    }
  }

  $foo = new db_foo;

?>

 

Theres other tricks too, like making it look through a specific set of paths until it finds a matching class etc etc.

 

Anyway, yeah, it comes in real handy.

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.