2

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

Yeak
  • 2,470
  • 9
  • 45
  • 71

2 Answers2

8

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');
René Höhle
  • 26,716
  • 22
  • 73
  • 82
3

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());
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • I definitely recommend using `set_include_path`. – Mike Mackintosh Jun 25 '12 at 16:50
  • so the only way to do it is using require? – Yeak Jun 25 '12 at 20:17
  • Well yes at some point youll always do a `require`. Now you could (and i would recommend) create or use an existing autoloading component like [Symfony's](http://symfony.com/doc/current/components/class_loader.html) or [Zend's](http://framework.zend.com/manual/en/zend.loader.autoloader.html), but internally its just going to map a class name to file and then require it. – prodigitalson Jun 25 '12 at 20:19
  • Ok thank you. I am currently using a autoload function which i wrote myself for instantiating a class – Yeak Jun 25 '12 at 22:36