I have two folders in my directory:
- Plugins
- Classes
The Plugins folder contains two files: Sample.php and Plugins.php.
Sample.php is just a class with one function that extends the Plugins class. The Plugins class tries to create a new instance of the base class which is located in the Classes folder.
Plugins/Sample.php:
class Sample extends Plugins {
public $eggs;
public $pounds;
public function __construct() {
$this->eggs = "100";
$this->pounds = "10";
}
public function OnCall() {
echo "{$this->eggs} eggs cost {$this->pounds} pounds, {$this->name}!";
}
}
Plugins/Plugins.php:
class Plugins {
public $name;
public function __construct() {
include '../Classes/Base.php';
$base = new Base();
$this->name = $base->name;
}
}
Classes/Base.php:
class Base {
public $name = "Will";
public function Say() {
echo $this->name;
}
}
Index.php includes everything in the Plugins folder and is supposed to execute OnCall(). It is giving the following error messages:
Warning: include(../Classes/Base.php) [function.include]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/Plugins/Plugins/Plugins.php on line 6
Warning: include() [function.include]: Failed opening '../Classes/Base.php' for inclusion (include_path='.:/Applications/XAMPP/xamppfiles/lib/php:/Applications/XAMPP/xamppfiles/lib/php/pear') in /Applications/XAMPP/xamppfiles/htdocs/Plugins/Plugins/Plugins.php on line 6
Fatal error: Class 'Base' not found in /Applications/XAMPP/xamppfiles/htdocs/Plugins/Plugins/Plugins.php on line 7
Index.php (if it helps):
foreach(glob('Plugins/*.php') as $file) {
require_once $file;
$class = basename($file, '.php');
if(class_exists($class)) {
$obj = new $class;
$obj->OnCall();
}
}
What I need to do is use the Base class in classes outside of the Classes folder. How can I do so?
Classes/Base.php
instead of a relative path. – Rocket Hazmatrequire_once 'Classes/Base.php'; $GLOBALS['base'] = new Base();
to index.php, and changing$this->base
to$GLOBALS['base']
and$this->name
to$this->base->name
in Plugins.php. It now outputs100 eggs cost 10 pounds, the 100 year old!
instead of100 eggs cost 10 pounds, Will the 100 year old!
. – James