简体   繁体   中英

PHP class extending another class outside of folder

I have a general class which i like to have it extended by other classes.

I have my directory set up with folders and class files inside of those folders for example

Classes/users/users.class.php classes/general/general.class.php

I have the users class extending the general class but since they are in different folders I guess the general class is not found.

class users extends general {

}

Can someone please help me out figuring this out.

I should also mention i am using autload function

When you have no autoloader then include the class before.

Then the class is known and you can use it.

require_once(__DIR__.'/../general/general.class.php');

You need to make sure you load both classes or any other class that are required. For example:

In your bootstrap...:

// Set up the include path so that we can easily access the class files
set_include_path('/absolute/path/to/classes'. PATH_SEPARATOR . get_include_path());
require_once('users/users.class.php');

In users.class.php:

require_once('general/general.class.php');
class User {
  // you class definition
}

As far as getting the absolute path to your classes folder, youll want to configure this based on your bootstrap location. For example:

// bootstrap lives in $_SERVER['DOCUMENT_ROOT'] and classes is one level up outside the doc root
// this code is in the bootstrap...
$path = realpath(dirname(__FILE__).'/../classes');
set_include_path($path . PATH_SEPARATOR . get_include_path());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM