PHP包含子类的路径

I'm having an issue with the include/require path. Since the code style is OOP, the problem comes when subclassing, since the path is relative to the first includer file. For example:

// path: /class/entity/A.class.php
class A { /*definition*/ }


// path: /class/widget/B.class.php
require_once("../model/entity/A.class.php");
class B extends A { /*definition*/ }


// path /ajax/some_request.php
require_once("../class/widget/B.class.php");
//.. some code to execute

The problem is that since the require path is relative to some_request.php, it won't find A.class.php because the path would end up being /model/entity/A.class.php where it should be /class/model/entity/A.class.php

You can use relative paths to do this (which you already have in there):

If you want to go up two directories you would do:

require_once("../../path/to/my/file.php");

From your structure it looks like you would want something like:

require_once("../../../classes/model/entity/A.cass.php");

Am I missing something from your question?

Use

$path = dirname(dirname(__FILE__))."/my/path";

to deference from the parent directory of the current file. To use your code as an example...

// path: /class/model/entity/A.class.php
class A { /*definition*/ }

// path: /class/widget/B.class.php
require_once(dirname(dirname(__FILE__))."/model/entity/A.class.php");
class B extends A { /*definition*/ }


// path /ajax/some_request.php
require_once(dirname(dirname(__FILE__))."/class/widget/B.class.php");
//.. some code to execute