1
votes

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?

2
Where's the code for your index.php? If that's the script showing an error and loading the classes, we'll need to see it.helion3
@helion3 Oops. Now added it along with the error messages.James
Try using an absolute path to Classes/Base.php instead of a relative path.Rocket Hazmat
I've fixed the including error, but it still won't display the name variable.James
I fixed it by adding require_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 outputs 100 eggs cost 10 pounds, the 100 year old! instead of 100 eggs cost 10 pounds, Will the 100 year old!.James

2 Answers

0
votes

You need to call the parent's constructor in your Sample class.

class Sample extends Plugins {

    public $eggs;
    public $pounds;

    public function __construct() {
        parent::__construct();
        $this->eggs   = "100";
        $this->pounds = "10";
    }

    public function OnCall() {
        echo "{$this->eggs} eggs cost {$this->pounds} pounds, {$this->name}!";
    }
}
0
votes

You probably want to take advantage of __autoload (http://ca1.php.net/manual/en/function.autoload.php)

Using this function will allow you to load up your classes easily no matter what directory they're in.

Simple Example:

function __autoload($classname) {
  $path = "path/to/Classes/$classname.php";
  if(file_exists($path)) {
    require_once $path;
  }
}

This means you would be able to remove your include statement from your Plugins class and simply keep the declaration $base = new Base(); and __autoload will be magically called and load up the correct file.