require_once is being a pain in the butt

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

require_once is being a pain in the butt

Post by Luke »

I have had this issue a few times and I can never remember what causes it. I need to write it down next time I figure it out. Anyway... I've been working on several libraries I'm releasing on google code. In these libraries, I've tried to include_once every file that a particular file is dependant on. For instance, in php csv utils, I have a class called Csv_Reader_String which extends Csv_Reader. The file looks like this:

notice the require_once '../Reader.php' - shouldn't this look in the directory above where the file is for a file called Reader.php and include it if it hasn't been already?

Code: Select all

<?php
require '../Reader.php';
class Csv_Reader_String extends Csv_Reader {
 
    /**
     * 
     */
    public function __construct($string, Csv_Dialect $dialect = null) {
    
        if (is_null($dialect)) {
            $dialect = $this->autoDetectFile($path);
        }
        $this->dialect = $dialect;
        // if last character isn't a line-break add one
        $lastchar = substr($string, strlen($string)-1, 1);
        if ($lastchar !== $dialect->lineterminator) $string = $string . $dialect->lineterminator;
        $this->handle = fopen("php://memory", 'w+'); // not sure if I should use php://memory or php://temp here
        fwrite($this->handle, $string);
        if ($this->handle === false) throw new Csv_Exception_FileNotFound('File does not exist or is not readable: "' . $path . '".');
        $this->rewind();
    
    }
 
}
With this code, I get Warning: require_once(../Reader.php) [function.require-once]: failed to open stream: No such file or directory in C:\wampalamp\www\svn\php-csv-utils\Csv\Reader\String.php on line 2

I don't get it. I look at that include path and I see "." (the current directory) as the first place it looks, and yet it says it can't find it. So I try "require_once 'Csv/Reader.php'" and I get:

Fatal error: Class 'Csv_Reader' not found in C:\wampalamp\www\svn\php-csv-utils\Csv\Reader\String.php on line 3

So I try "require 'Csv/Reader.php'" and I get:

Fatal error: Cannot redeclare class Csv_Reader in C:\wampalamp\www\svn\php-csv-utils\Csv\Reader.php on line 27

What in the world is going on??
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: require_once is being a pain in the butt

Post by Luke »

I got it fixed, but I honestly don't know how. :(
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: require_once is being a pain in the butt

Post by Luke »

Hmm... it may have had to do with my include path having "C:\" in one spot and "c:\" in another. Maybe.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: require_once is being a pain in the butt

Post by Weirdan »

paths in include_path are evaluated relative to current working dir (getpwd()), not relative to the file that has control at the moment. This should help:

Code: Select all

 
require_once dirname(__FILE__) . '/../Reader.php';
 
But using dynamically constructed paths tends to confuse opcode caches. As of php 5.3 you may use __DIR__ instead of dirname(__FILE__) - this makes path to be known at compile time.

Another option is to have a constant that refers to the root of your library file structure, and include files like this:

Code: Select all

 
require_once CSV_LIBRARY_ROOT . '/CSV/Reader.php';
 
Post Reply